如何通过Web服务在Asp.Net中发送电子邮件和消息 [英] How Can I Send Email And Messages Via Web Services In Asp.Net

查看:60
本文介绍了如何通过Web服务在Asp.Net中发送电子邮件和消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我我的问题的答案...

解决方案

回答:如果您有发送邮件的Web服务,请使用其API并按照它进行操作。但通常情况下,邮件是使用特殊服务发送的,使用专门为邮件创建和标准化的协议,例如SMTP。



-SA

以下代码示例演示如何创建和发送带附件的电子邮件。在您的界面中添加此方法签名并以这种方式实现:

  public   static   void  CreateMessageWithAttachment( string  server)
{
// 指定要附加和发送的文件。
// 此示例假定
// 当前工作目录。
string file = data.xls;
// 创建邮件并设置收件人。
MailMessage message = new MailMessage(
jane @ contoso。 com
ben@contoso.com
季度数据报告。
请参阅随附的电子表格。);

// 为此电子邮件创建文件附件。
附件数据= 附件(文件,MediaTypeNames.Application.Octet);
// 添加文件的时间戳信息。
ContentDisposition disposition = data .ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// 将文件附件添加到此电子邮件中。
message .Attachments.Add(数据);

// 发送消息。
SmtpClient client = < span class =code-keyword> new
SmtpClient(server);
// 如果SMTP服务器需要凭据,则添加凭据。
client.Credentials = CredentialCache.DefaultNetworkCredentials;

尝试 {
client.Send(message);
}
catch (Exception ex){
Console.WriteLine( 在CreateMessageWithAttachment()中捕获异常:{0}
ex.ToString());
}
// 显示附件的ContentDisposition中的值。
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine( 内容处置);
Console.WriteLine(cd.ToString());
Console.WriteLine( File {0},cd.FileName);
Console.WriteLine( Size {0},cd.Size);
Console.WriteLine( Creation {0},cd.CreationDate);
Console.WriteLine( Modification {0},cd.ModificationDate);
Console.WriteLine( Read {0},cd.ReadDate);
Console.WriteLine( Inline {0},cd.Inline);
Console.WriteLine( 参数:{0},cd.Parameters.Count) ;
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine( {0} = {1},d.Key,d.Value) ;
}
data.Dispose();
}





MSDN [ ^ ]:D;)



-KR


Please tell me answer to my question...

解决方案

Answering: if you have a Web service sending mails, take its API and follow it. But usually, mail is sent using special services using protocols created and standardized specially for mail, such as SMTP.

—SA


The following code example demonstrates creating and sending an e-mail message with an attachment. Add this method signature in your interface and implement it like this way :

public static void CreateMessageWithAttachment(string server)
		{
			// Specify the file to be attached and sent. 
			// This example assumes that a file named Data.xls exists in the 
			// current working directory. 
			string file = "data.xls";
			// Create a message and set up the recipients.
			MailMessage message = new MailMessage(
			   "jane@contoso.com",
			   "ben@contoso.com",
			   "Quarterly data report.",
			   "See the attached spreadsheet.");

			// Create  the file attachment for this e-mail message.
			Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
			// Add time stamp information for the file.
			ContentDisposition disposition = data.ContentDisposition;
			disposition.CreationDate = System.IO.File.GetCreationTime(file);
			disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
			disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
			// Add the file attachment to this e-mail message.
			message.Attachments.Add(data);

			//Send the message.
			SmtpClient client = new SmtpClient(server);
			// Add credentials if the SMTP server requires them.
			client.Credentials = CredentialCache.DefaultNetworkCredentials;

      try {
			  client.Send(message);
			}
			catch (Exception ex) {
			  Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                    ex.ToString() );			  
			}
			// Display the values in the ContentDisposition for the attachment.
			ContentDisposition cd = data.ContentDisposition;
			Console.WriteLine("Content disposition");
			Console.WriteLine(cd.ToString());
			Console.WriteLine("File {0}", cd.FileName);
			Console.WriteLine("Size {0}", cd.Size);
			Console.WriteLine("Creation {0}", cd.CreationDate);
			Console.WriteLine("Modification {0}", cd.ModificationDate);
			Console.WriteLine("Read {0}", cd.ReadDate);
			Console.WriteLine("Inline {0}", cd.Inline);
			Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
			foreach (DictionaryEntry d in cd.Parameters)
			{
				Console.WriteLine("{0} = {1}", d.Key, d.Value);
			}
			data.Dispose();
		}



Ripped from MSDN[^] :D ;)

-KR


这篇关于如何通过Web服务在Asp.Net中发送电子邮件和消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆