方法体: public static void SendMessage(Demand demand, string type, string Message)
{
StringBuilder sb = new StringBuilder();
MailAddress ma = new MailAddress("[email protected]", "适配&批次管理系统", System.Text.Encoding.UTF8);
MailMessage msg = new MailMessage();
msg.From = ma;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
if (type == "New")
{
UserInfo user = UserInfoManager.GetUserInfoByUserID(demand.AssignedClient);
UserInfo saler = UserInfoManager.GetUserInfoByUserID(demand.Userid);
sb.AppendLine("新需求!请查阅!");
sb.AppendLine("需求ID为:" + demand.ID);
sb.AppendLine("厂商:" + demand.OriginName);
sb.AppendLine("BOX版本:" + demand.BoxID);
sb.AppendLine("平台:" + demand.PlatID);
sb.AppendLine("软件版本:" + demand.SysPlatName);
sb.AppendLine("硬件版本:" + demand.SysPlatID);
sb.AppendLine("项目名称:" + demand.ProjectName);
sb.AppendLine("分辨率:" + demand.ResolutionID);
sb.AppendLine("厂商技术:" + demand.OriginTec);
sb.AppendLine("厂商技术联系方式:" + demand.OriginContacts);
sb.AppendLine("需要内置的游戏:" + demand.InlayGame);
sb.AppendLine("与厂商沟通的收费方式:" + demand.OriginBillingType);
sb.AppendLine("G游戏精灵游戏:" + demand.GGameID);
sb.AppendLine("厂商特殊修改内容:" + demand.OriginSpecialContent);
sb.AppendLine("预约现成移植时间:" + demand.AppointmentDate);
sb.AppendLine("项目截止时间:" + demand.ProjectEndDate);
sb.AppendLine("备注:" + demand.Note);
sb.AppendLine("指派客户端负责人:"+user.Name);
sb.AppendLine("创建人:" + saler.Name);
sb.AppendLine("创建时间:" + demand.CreateTime.ToString());
msg.Subject = "新需求!";
msg.To.Add(new MailAddress(user.Email));
msg.To.Add(new MailAddress("[email protected]"));
}
msg.Body = sb.ToString();
msg.IsBodyHtml = false;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
SmtpClient smtp = new SmtpClient();
smtp.Host = "邮件服务器地址";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("。", "");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
调用方式:ThreadPool.QueueUserWorkItem(o => CheckMao.SendMail.SendMessage(demand, "New", null));
大家看看有什么问题吗?邮件发不出去。不知道为什么
{
StringBuilder sb = new StringBuilder();
MailAddress ma = new MailAddress("[email protected]", "适配&批次管理系统", System.Text.Encoding.UTF8);
MailMessage msg = new MailMessage();
msg.From = ma;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
if (type == "New")
{
UserInfo user = UserInfoManager.GetUserInfoByUserID(demand.AssignedClient);
UserInfo saler = UserInfoManager.GetUserInfoByUserID(demand.Userid);
sb.AppendLine("新需求!请查阅!");
sb.AppendLine("需求ID为:" + demand.ID);
sb.AppendLine("厂商:" + demand.OriginName);
sb.AppendLine("BOX版本:" + demand.BoxID);
sb.AppendLine("平台:" + demand.PlatID);
sb.AppendLine("软件版本:" + demand.SysPlatName);
sb.AppendLine("硬件版本:" + demand.SysPlatID);
sb.AppendLine("项目名称:" + demand.ProjectName);
sb.AppendLine("分辨率:" + demand.ResolutionID);
sb.AppendLine("厂商技术:" + demand.OriginTec);
sb.AppendLine("厂商技术联系方式:" + demand.OriginContacts);
sb.AppendLine("需要内置的游戏:" + demand.InlayGame);
sb.AppendLine("与厂商沟通的收费方式:" + demand.OriginBillingType);
sb.AppendLine("G游戏精灵游戏:" + demand.GGameID);
sb.AppendLine("厂商特殊修改内容:" + demand.OriginSpecialContent);
sb.AppendLine("预约现成移植时间:" + demand.AppointmentDate);
sb.AppendLine("项目截止时间:" + demand.ProjectEndDate);
sb.AppendLine("备注:" + demand.Note);
sb.AppendLine("指派客户端负责人:"+user.Name);
sb.AppendLine("创建人:" + saler.Name);
sb.AppendLine("创建时间:" + demand.CreateTime.ToString());
msg.Subject = "新需求!";
msg.To.Add(new MailAddress(user.Email));
msg.To.Add(new MailAddress("[email protected]"));
}
msg.Body = sb.ToString();
msg.IsBodyHtml = false;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
SmtpClient smtp = new SmtpClient();
smtp.Host = "邮件服务器地址";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("。", "");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
调用方式:ThreadPool.QueueUserWorkItem(o => CheckMao.SendMail.SendMessage(demand, "New", null));
大家看看有什么问题吗?邮件发不出去。不知道为什么
using System.Net.Mail;
using System.Net;
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="to"></param>
/// <param name="from"></param>
/// <param name="subject">主题</param>
/// <param name="body">正文</param>
/// <param name="userName">发件用户名</param>
/// <param name="password">发件密码</param>
/// <param name="smtpHost"></param>
public static bool Send(string to, string from, string subject, string body, string userName, string password, string smtpHost, string CC)
{
try
{
MailAddress From = new MailAddress(from);
MailAddress To = new MailAddress(to);
MailMessage message = new MailMessage(From, To);
message.Subject = subject;//设置邮件主题
message.IsBodyHtml = true;//设置邮件正文为html格式
message.Body = body;//设置邮件内容
if (CC != null && CC != "")
message.CC.Add(CC);//抄送
message.Priority = MailPriority.High;//优先级
// message.Attachments.Add(new Attachment ( @"/new1/photo/small/103427.jpg"));//附件
SmtpClient client = new SmtpClient(smtpHost);
//设置发送邮件身份验证方式
//注意如果发件人地址是[email protected],则用户名是abc而不是[email protected]
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Send(message);
message.Dispose();
return true;
}
catch (Exception ex)
{
return false;
}
}
{
bool IsSend = false;
string MailFrom = "";
string MailFromPassword = "";
string DisplayName = "";
try
{
SmtpClient smtpServer = new SmtpClient();
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.EnableSsl = false;
smtpServer.Host = "";
smtpServer.UseDefaultCredentials = true;
smtpServer.Credentials = new NetworkCredential(MailFrom,MailFromPassword);
MailMessage mailMessage = new MailMessage();
mailMessage.Priority = MailPriority.High;
mailMessage.From = new MailAddress(MailFrom, DisplayName, Encoding.GetEncoding(936));
mailMessage.ReplyTo = new MailAddress(MailFrom, "我的接收邮箱", Encoding.GetEncoding(936));
foreach (string MailList in MailToList)
{
mailMessage.To.Add(new MailAddress(MailList, MailList, Encoding.GetEncoding(936)));
}
mailMessage.Subject = Title;
mailMessage.SubjectEncoding = Encoding.GetEncoding(936);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = Encoding.GetEncoding(936);
mailMessage.Body = Body;
smtpServer.Send(mailMessage);
IsSend = true;
}
catch
{
IsSend = false;
}
return IsSend;
}
private void button1_Click(object sender, EventArgs e)
{ string fjrtxt = fjr.Text; string mmtxt = mm.Text; string sjrtxt = sjr.Text; string zttxt = zt.Text; string fjtxt = fj.Text; string nrtxt = nr.Text;
string[] fasong = fjrtxt.Split('@'); string[] fs = fasong[1].Split('.');
//发送 SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com"); //设置邮件协议 client.UseDefaultCredentials = false;//这一句得写前面 client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器 client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt); //通过用户名和密码 认证
MailMessage mmsg = new MailMessage(new MailAddress(fjrtxt), new MailAddress(sjrtxt)); //发件人和收件人的邮箱地址 mmsg.Subject = zttxt; //邮件主题 mmsg.SubjectEncoding = Encoding.UTF8; //主题编码 mmsg.Body = nrtxt; //邮件正文 mmsg.BodyEncoding = Encoding.UTF8; //正文编码 mmsg.IsBodyHtml = true; //设置为HTML格式 mmsg.Priority = MailPriority.High; //优先级
if (fj.Text.Trim() != "")
{ mmsg.Attachments.Add(new Attachment(fj.Text));//增加附件 } try
{ client.Send(mmsg); MessageBox.Show("邮件已发成功"); } catch (Exception ex)
{ MessageBox.Show(ex.Message); } }
MailAddress ma = new MailAddress("[email protected]", "适配&批次管理系统", System.Text.Encoding.UTF8);
MailMessage msg = new MailMessage();
msg.From = ma; 发件人的用户名以及密码是略去了还是根本就没设置?还有
smtp.Host = "邮件服务器地址";
端口没设置就是默认的25
你try下看报的什么错?
在cmd中运行telnet 邮件服务器地址
>>返回OK后继续执行
user pici
>>如果该用户名登录成功,POP3服务器会响应如下信息:+OK core mail 接着马上输入密码
pass XXXXX
>>如果成功,会显示+OK...http://hi.baidu.com/8love/blog/item/abb5d7c8afcfb1177f3e6fac.html如果telnet都登陆不上去,你就先不要管程序了,先问问那台服务器是否已经开放了SMTP外发功能吧