完整代码(发送QQ邮件需要手动设置smtp)(使用QQ邮箱无法发送邮件则需要在QQ邮箱里设置一下,见图)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Mail; namespace EmailSendAndReceive { public partial class frmSend : Form { public frmSend() { InitializeComponent(); } //对邮件内容进行编码 public static string Base64Encode(string str) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); } private void btnSend_Click(object sender, EventArgs e) { //try //{ MailAddress from = new MailAddress("zhangyong_890910@163.com"); //设置邮件发送人 MailAddress to = new MailAddress("524958043@qq.com"); //设置邮件接收人 MailMessage message = new MailMessage(from, to); //实例化一个MaileMessage类对象 message.Subject = Base64Encode(txtSubject.Text); //设置发送邮件的主题 message.Body = Base64Encode(txtContent.Text); //设置发送邮件的内容 message.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.Host = "smtp.163.com";//若发送QQ邮件则将此处改为"smtp.qq.com" client.Port = 25; //设置用于验证发件人身份的凭据 client.Credentials = new System.Net.NetworkCredential("zhangyong_890910@163.com", "zy1111020"); //发送邮件 client.Send(message); MessageBox.Show("发送成功"); //} //catch //{ // MessageBox.Show("发送失败!"); //} } } }
使用QQ邮箱无法发送邮件则需要在QQ邮箱里设置一下,见图