这个发送邮件的函数/// <summary>发送email,默认是25端口
///
/// </summary>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAdress">收件人</param>
/// <param name="fromAdress">发件人</param>
/// <param name="userName">发件用户名</param>
/// <param name="userPwd">发件密码</param>
/// <param name="smtpHost">smtp地址</param>
public static void SendMail(string title, string body, string toAdress, string fromAdress,string userName, string userPwd, string smtpHost)
{
try
{
MailAddress to = new MailAddress(toAdress);
MailAddress from = new MailAddress(fromAdress);
Encoding chtEnc = Encoding.GetEncoding(950);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.IsBodyHtml = true; // 如果不加上这句那发送的邮件内容中有HTML会原样输出
message.Subject = title; message.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Port = 25;
smtp.Credentials = new NetworkCredential(userName, userPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = smtpHost;
message.To.Add(toAdress);
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
这个是调用方法时给的参数
SendMail("发邮件测试看看", "哟哟!神机小徐来也,换一换脑袋!", "[email protected]", "[email protected]", "888888888", "88888888", "smtp.qq.com");但是每次都出现这个问题在邮件标头中找到无效的字符。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 在邮件标头中找到无效的字符。
///
/// </summary>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAdress">收件人</param>
/// <param name="fromAdress">发件人</param>
/// <param name="userName">发件用户名</param>
/// <param name="userPwd">发件密码</param>
/// <param name="smtpHost">smtp地址</param>
public static void SendMail(string title, string body, string toAdress, string fromAdress,string userName, string userPwd, string smtpHost)
{
try
{
MailAddress to = new MailAddress(toAdress);
MailAddress from = new MailAddress(fromAdress);
Encoding chtEnc = Encoding.GetEncoding(950);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.IsBodyHtml = true; // 如果不加上这句那发送的邮件内容中有HTML会原样输出
message.Subject = title; message.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Port = 25;
smtp.Credentials = new NetworkCredential(userName, userPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = smtpHost;
message.To.Add(toAdress);
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
这个是调用方法时给的参数
SendMail("发邮件测试看看", "哟哟!神机小徐来也,换一换脑袋!", "[email protected]", "[email protected]", "888888888", "88888888", "smtp.qq.com");但是每次都出现这个问题在邮件标头中找到无效的字符。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 在邮件标头中找到无效的字符。
解决方案 »
- js遇到个麻烦的事情。
- .net 学什么
- vs2008如何调出Chart图表控件?
- asp.net 怎么获取TreeView树形结构控件选定节点的“文本”路径 急
- 使用模版页后如何高效率的联结自己的js?
- 问个简单问题
- treeview框架页面的传值奇怪问题
- 无法将类型为“System.Security.Principal.WindowsPrincipal”的对象强制转换为类型“Business.PhilePrincipal”
- 在B/S模式下,有没有办法取得客户端的网卡MAC,CPU序列号等信息?
- 一个关于数组的问题!!
- 求教一个关于imagebutton控件的问题
- 怎样动态的增加sql语句中的条件
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;namespace MailSender
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment; // 创建一个附件对象
objMailAttachment = new MailAttachment("C:\\1.xml");//发送邮件的附件 // 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "[email protected]";//源邮件地址
objMailMessage.To = "********@qq.com";//目的邮件地址
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中 //接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "mytest110"); //密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******"); //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为:530 Authentication required //SMTP地址
SmtpMail.SmtpServer = "smtp.sina.com"; // 开始发送邮件
// 在发送之前,去新浪邮箱里开启POP/SMTP设置 邮箱设置->账户->POP/SMTP设置->开启
// 否则会报错误0x80040217. The server response was not available
SmtpMail.Send(objMailMessage);
}
}
}
MSDN解决方案