邮件发送程序

/// <summary>
/// 发送邮件方法1
/// </summary>
/// <param name="emailTo">接收邮件地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <returns>发送结果</returns>
public static string SendEmail(string emailTo, string subject, string body)
{
MailMessage mailMsg = new MailMessage();
//设置正文格式
mailMsg.BodyFormat = MailFormat.Html;
//设置收件人的邮件地址
mailMsg.To = emailTo;
//设置发送者的邮件地址
mailMsg.From = "Your@163.com";
//设置邮件主题
mailMsg.Subject = subject;
//设置邮件内容
mailMsg.Body = body;
//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "YourName");
//设置用户密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "YourPassword");
try
{
//设置发送邮件服务器
SmtpMail.SmtpServer = "SMTP.163.com";
//发送邮件
SmtpMail.Send(mailMsg);

return "您的新密码已经发送到你的注册邮箱里,请注意查收!";

}
catch (Exception err)
{
//Response.Write("发送失败" + err.Message.ToString());
return "邮件发送失败" + err.Message.ToString();
}
}
 

方法2 

MailMessage mailMessage = new MailMessage();
mailMessage.To = "1@163.com";//收件人名
mailMessage.From = "1@163.com";//发件人
mailMessage.Subject = "标题";
mailMessage.Body = strMailMessage;//邮件内容
mailMessage.BodyFormat = MailFormat.Html;
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );     //认证类型
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "1@163.com" );//要认证的用户名
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456" );    //要认证的密码
string smtpServer = "smtpx.163vip.net";//163邮箱
SmtpMail.SmtpServer= smtpServer;
SmtpMail.Send( mailMessage );}
catch( Exception E )
{
MessageBox.Show( E.Message );
return;
}

方法3代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Web.Util;

public partial class _Default : System.Web.UI.Page
{
public void Page_Load()
{
MailMessage myMail = new MailMessage();
myMail.From ="flyzph@163.com";
SmtpMail.SmtpServer = "smtp.163.com";
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxx@163.com"); //set your username here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxx的密码"); //set your password here
myMail.To = "收信人邮箱" ;
myMail.Subject = "helloxx;
myMail.Body = "this is the content!";
SmtpMail.Send(myMail);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值