日期:2014-05-18 浏览次数:20926 次
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");
MailAddre