当前位置: 编程技术>.net/c#/asp.net
c#使用netmail发送邮件的例子
来源: 互联网 发布时间:2014-08-30
本文导语: 例子,c#使用netmail方式发送邮件。 代码示例: /// /// NetMail方式测试通过 /// private void TestSend() { System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(); //收件人地址 mm.To.Add(new System.Net.Mail.MailAddress("xxxxxx@163.com", "Name")); //...
例子,c#使用netmail方式发送邮件。
代码示例:
///
/// NetMail方式测试通过
///
private void TestSend()
{
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
//收件人地址
mm.To.Add(new System.Net.Mail.MailAddress("xxxxxx@163.com", "Name"));
//发件人地址
mm.From = new System.Net.Mail.MailAddress("xxxxx@sina.com");
//这个可以不指定
//mm.Sender = new System.Net.Mail.MailAddress("xxx@sina.com", "SenderName");、
mm.Subject = "This is Test Email";
mm.Body = "This is Testing SMTP Mail Send By Me";
mm.IsBodyHtml = true;
mm.Priority = System.Net.Mail.MailPriority.High; // 设置发送邮件的优先级
System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
//指定邮件服务器
smtCliend.Host = "smtp.sina.com";
//smtp邮件服务器的端口号
smtCliend.Port = 25;
//设置发件人邮箱的用户名和地址,使用公共邮件服务器一般需要提供,不然发送不会成功
smtCliend.Credentials = new NetworkCredential("xxxxxxx", "xxxxxxx");
//指定邮件的发送方式
smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
smtCliend.Send(mm);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
/// NetMail方式测试通过
///
private void TestSend()
{
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
//收件人地址
mm.To.Add(new System.Net.Mail.MailAddress("xxxxxx@163.com", "Name"));
//发件人地址
mm.From = new System.Net.Mail.MailAddress("xxxxx@sina.com");
//这个可以不指定
//mm.Sender = new System.Net.Mail.MailAddress("xxx@sina.com", "SenderName");、
mm.Subject = "This is Test Email";
mm.Body = "This is Testing SMTP Mail Send By Me";
mm.IsBodyHtml = true;
mm.Priority = System.Net.Mail.MailPriority.High; // 设置发送邮件的优先级
System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
//指定邮件服务器
smtCliend.Host = "smtp.sina.com";
//smtp邮件服务器的端口号
smtCliend.Port = 25;
//设置发件人邮箱的用户名和地址,使用公共邮件服务器一般需要提供,不然发送不会成功
smtCliend.Credentials = new NetworkCredential("xxxxxxx", "xxxxxxx");
//指定邮件的发送方式
smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
smtCliend.Send(mm);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}