C#用smtp发送邮件问题
想问问..C#用smtp发送邮件,如何验证发送人的账户密码是否正确?
[解决办法]
SmtpClient smtp //如果需要认证,则用下面的方式smtp.Credentials = new NetworkCredential("user", "pwd");
[解决办法]
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace SendEmail
{
public partial class EmailSender : Form
{
string regStr =string.Empty;
string from = string.Empty;
string to = string.Empty;
string body = string.Empty;
const string FROMEMAIL = "wmglove@sina.com.cn";
const string FROMPWD = "不给你说";
string pwd = string.Empty;
public EmailSender()
{
InitializeComponent();
regStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
cmbBox.SelectedIndex = 0;
}
private void btnSender_Click(object sender, EventArgs e)
{
to = txtBoxRecv.Text.Trim();
body = txtBoxBody.Text.Trim();
if (to.Length == 0) return;
if (body.Length == 0) return;
if (!Regex.IsMatch(to, regStr))
{
MessageBox.Show("接收的电子邮件的格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
from = txtBoxSender.Text.Trim();
pwd = txtBoxPwd.Text.Trim();
if (from.Length == 0)
{
SendEmail(FROMEMAIL, FROMPWD, to, FROMEMAIL, body, "smtp.sina.com.cn");
}
else
{
if (pwd.Length == 0)
{
MessageBox.Show("电子邮件的密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (Regex.IsMatch(from, regStr))
{
switch (cmbBox.SelectedItem.ToString())
{
case "QQ.com":
SendEmail(from, pwd, to, from, body, "smtp.qq.com.cn");
break;
case "163.com":
SendEmail(from, pwd, to, from, body, "smtp.163.com.cn");
break;
case "126.com":
SendEmail(from, pwd, to, from, body, "smtp.126.com.cn");
break;
case "sina.com":
SendEmail(from, pwd, to, from, body, "smtp.sina.com.cn");
break;
default:
break;
}
}
else
{
MessageBox.Show("发送的电子邮件的格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private void SendEmail(string from, string fromPwd, string to, string subject, string body,string emailType)
{
string attFile = txtBoxFile.Text.Trim();
ContentDisposition cd;
MailAddress addrFrom = new MailAddress(from, "from");
MailAddress addrTo = new MailAddress(to, "to");
Attachment att = new Attachment(attFile, MediaTypeNames.Application.Octet);
MailMessage mm = new MailMessage(addrFrom, addrTo);
mm.BodyEncoding = Encoding.UTF8;
mm.IsBodyHtml = true;
mm.Subject = subject;
mm.Body = body;
if (attFile != string.Empty)
{
cd = att.ContentDisposition;
cd.CreationDate = File.GetCreationTime(attFile);
cd.ModificationDate = File.GetLastWriteTime(attFile);
cd.ReadDate = File.GetLastAccessTime(attFile);
mm.Attachments.Add(att);//添加附件
}
NetworkCredential nc = new NetworkCredential(from, fromPwd);
SmtpClient smtp = new SmtpClient(emailType);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mm);
}
private void btnFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "(所有文件)*.*|*.*";
ofd.FilterIndex = 0;
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
ofd.RestoreDirectory = true;
if (DialogResult.OK == ofd.ShowDialog())
txtBoxFile.Text = ofd.FileName;
}
}
}