[100分]急急急急急急急急!!!!局域网发邮件失败 本帖最后由 gz5182009 于 2011-06-28 15:04:49 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 发送邮件方法 /// </summary> /// <param name="username">发送邮件方的登陆用户名(注:这里并不是邮箱名称,而是邮件@前的名称)</param> /// <param name="pwd">登陆密码</param> /// <param name="server">SMTP传输协议服务器地址(发送邮件方的服务器地址)</param> /// <param name="fromemail">发送邮件方的邮箱账号</param> /// <param name="tomail">接收邮件方的邮箱账号</param> /// <param name="subject">邮件主题</param> /// <param name="body">邮件正文内容</param> /// <param name="fileUpLoad">邮件附件(上传到服务器的Excel文件)</param> /// <returns>成功返回 True / 失败返回 False</returns> private bool SendEmail(string username, string pwd, string server, string fromemail, string tomail, string subject, string body, string fileUpLoad) { bool isFlag = true; try { //创建传输协议smtp对象 System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); //创建发送邮件类对象 System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); //发送邮件方 mail.From = new System.Net.Mail.MailAddress(fromemail); //接收邮件方 mail.To.Add(new System.Net.Mail.MailAddress(tomail)); //附件文件 mail.Attachments.Add(new Attachment(fileUpLoad)); //邮件主题 mail.Subject = subject; //正文内容 mail.Body = body; //是否Html格式 mail.IsBodyHtml = true; //SMTP服务器地址 smtp.Host = server; //发送连接时间 smtp.Timeout = int.MaxValue; smtp.UseDefaultCredentials = false; //发送端口 smtp.Port = 587; //是否加密 smtp.EnableSsl = true; //发送邮件方的身份验证(登陆名称/密码) smtp.Credentials = new System.Net.NetworkCredential(username, pwd); //发送邮件对象 smtp.Send(mail); } catch { isFlag = false; } return isFlag; } 这是错误解读 自己看看 try { System.Web.Mail.MailMessage mm=new System.Web.Mail.MailMessage(); mm.BodyFormat=System.Web.Mail.MailFormat.Html; mm.From="[email protected]"; mm.To=To; mm.BodyEncoding=System.Text.Encoding.GetEncoding(936); mm.Subject="您好!我是梦猫.NET工作室希望与您携手一起成长。"; mm.Body=Body; mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2; mm.Fields ["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"] = "[email protected]";//发送地址;如果mm.From写了这儿可以不写这句 mm.Fields ["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"] = "[email protected]"; mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "XXX";//验证账号:发送者邮箱账号 mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "XXXPASS"; //验证密码:发送者邮箱密码 mm.Fields ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; // 验证级别0,1,2 mm.Fields["http://schemas.microsoft.com/cdo/configuration/languagecode"] = 0x0804;//语言代码 mm.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "SMTP.XXX.com"; //SMTP Server System.Web.Mail.SmtpMail.SmtpServer="SMTP.XXX.com";//上句和这句重着,这 句可以替代上句 System.Web.Mail.SmtpMail.Send(mm); return 0; } catch(System.Exception e) { Response.Write(e.Message+e.StackTrace+e.Source); return -1; } } /// <summary> /// 邮件发送 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSendEmail_Click(object sender, EventArgs e) { string _x_ccono = X_CCONo.Text.Trim(); if (!_x_ccono.Equals(string.Empty)) { XJY_YS.BLL.CF.C_Complaints complaintsBll = new XJY_YS.BLL.CF.C_Complaints(); XJY_YS.Model.CF.C_Complaints complaintsModel = complaintsBll.GetModel(_x_ccono); if (complaintsModel != null) { CreateExcel(complaintsModel);//创建Excel GC.Collect(); //Kill进程 //主要是这段邮件内容调用 try { string from = FromName; string password = FromPass; string to = ToName; //取出用户名 int nameLength = from.IndexOf('@'); string username = from.Substring(0, nameLength); string subject = "";//主题 string body = "";//内容 string server = "smtp.gmail.com";//服务器地址 SMTP (这里是谷歌邮箱的) string fileEndName = "文件.xls"; string fileUpLoad = Server.MapPath("~/EmailSend/" + fileEndName); SendEmail(username, password, server, from, to, subject, body, fileUpLoad); } catch { // } } } }这里是用 谷歌邮箱 的作为服务器 smtp,Lz试试吧,俺的代码,不是copy的 急救!!RadComboBox插入Item后,ItemsRequested事件不能触发了 asp.net中的打印问题?? 紧急求救:自动生成的DataGrid的问题,谢谢!!! 用update批理插入数据无效的问题 又不会了,求教! 100分!找一款控件:在WEB中使用WORD编辑然后保存至服务端 .net中ajax的timer问题 请各位大哥大姐小弟小妹帮帮忙,在线等待 根据IP获取该IP下的主机头 web.config中的数据库路径问题及怎样简化数据库的链接 关于DotNetTextBox控件和ascx控件的问题。 问一个取文件属性的问题
/// 发送邮件方法
/// </summary>
/// <param name="username">发送邮件方的登陆用户名(注:这里并不是邮箱名称,而是邮件@前的名称)</param>
/// <param name="pwd">登陆密码</param>
/// <param name="server">SMTP传输协议服务器地址(发送邮件方的服务器地址)</param>
/// <param name="fromemail">发送邮件方的邮箱账号</param>
/// <param name="tomail">接收邮件方的邮箱账号</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文内容</param>
/// <param name="fileUpLoad">邮件附件(上传到服务器的Excel文件)</param>
/// <returns>成功返回 True / 失败返回 False</returns>
private bool SendEmail(string username, string pwd, string server, string fromemail, string tomail, string subject, string body, string fileUpLoad)
{
bool isFlag = true;
try
{
//创建传输协议smtp对象
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); //创建发送邮件类对象
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//发送邮件方
mail.From = new System.Net.Mail.MailAddress(fromemail);
//接收邮件方
mail.To.Add(new System.Net.Mail.MailAddress(tomail));
//附件文件
mail.Attachments.Add(new Attachment(fileUpLoad));
//邮件主题
mail.Subject = subject;
//正文内容
mail.Body = body;
//是否Html格式
mail.IsBodyHtml = true;
//SMTP服务器地址
smtp.Host = server;
//发送连接时间
smtp.Timeout = int.MaxValue;
smtp.UseDefaultCredentials = false;
//发送端口
smtp.Port = 587;
//是否加密
smtp.EnableSsl = true;
//发送邮件方的身份验证(登陆名称/密码)
smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
//发送邮件对象
smtp.Send(mail);
}
catch
{
isFlag = false;
}
return isFlag;
}
System.Web.Mail.MailMessage mm=new System.Web.Mail.MailMessage();
mm.BodyFormat=System.Web.Mail.MailFormat.Html;
mm.From="[email protected]";
mm.To=To;
mm.BodyEncoding=System.Text.Encoding.GetEncoding(936);
mm.Subject="您好!我是梦猫.NET工作室希望与您携手一起成长。";
mm.Body=Body;
mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
mm.Fields ["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"] = "[email protected]";//发送地址;如果mm.From写了这儿可以不写这句
mm.Fields ["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"] = "[email protected]";
mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "XXX";//验证账号:发送者邮箱账号
mm.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "XXXPASS"; //验证密码:发送者邮箱密码
mm.Fields ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; // 验证级别0,1,2
mm.Fields["http://schemas.microsoft.com/cdo/configuration/languagecode"] = 0x0804;//语言代码
mm.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "SMTP.XXX.com"; //SMTP Server
System.Web.Mail.SmtpMail.SmtpServer="SMTP.XXX.com";//上句和这句重着,这 句可以替代上句
System.Web.Mail.SmtpMail.Send(mm);
return 0;
}
catch(System.Exception e)
{
Response.Write(e.Message+e.StackTrace+e.Source);
return -1;
}
}
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSendEmail_Click(object sender, EventArgs e)
{
string _x_ccono = X_CCONo.Text.Trim();
if (!_x_ccono.Equals(string.Empty))
{
XJY_YS.BLL.CF.C_Complaints complaintsBll = new XJY_YS.BLL.CF.C_Complaints();
XJY_YS.Model.CF.C_Complaints complaintsModel = complaintsBll.GetModel(_x_ccono);
if (complaintsModel != null)
{
CreateExcel(complaintsModel);//创建Excel
GC.Collect(); //Kill进程 //主要是这段邮件内容调用
try
{
string from = FromName;
string password = FromPass;
string to = ToName;
//取出用户名
int nameLength = from.IndexOf('@');
string username = from.Substring(0, nameLength); string subject = "";//主题
string body = "";//内容
string server = "smtp.gmail.com";//服务器地址 SMTP (这里是谷歌邮箱的)
string fileEndName = "文件.xls";
string fileUpLoad = Server.MapPath("~/EmailSend/" + fileEndName); SendEmail(username, password, server, from, to, subject, body, fileUpLoad);
}
catch
{
//
}
}
}
}
这里是用 谷歌邮箱 的作为服务器 smtp,Lz试试吧,俺的代码,不是copy的