smtpClient发邮件错误:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
来源:互联网 发布:vb.net 资源文件exe 编辑:程序博客网 时间:2024/05/17 06:03
使用smtpClient发送邮件时,经常跑出难以明白的错误。其实主要都和我们申请的免费邮箱有关系。有些邮箱不支持,比如163的以前申请的帐号可以用,新注册的帐号是发不了的。
/**//// <summary>
/// 功能:发送邮件,返回字符串:“发送成功”否则返回错误代码。
/// 创建:2007-08-23
/// </summary>
/// <param name="MailTo">MailTo为收信人地址</param>
/// <param name="Subject">Subject为标题</param>
/// <param name="Body">Body为信件内容</param>
/// <param name="BodyFormat">BodyFormat为信件内容格式:0为Text,1为Html</param>
/// <param name="Priority">Priority为优先级:0为低,1为中,2为高</param>
/// <param name="Attachments">Attachment为附件,为null则不发送</param>
public static string SendMail(System.Collections.ArrayList MailTo, string Subject, string Body, int BodyFormat, int Priority, string Attachments)
{
string result;
SmtpClient mail = new SmtpClient();
//发送方式
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp服务器
mail.Host = "smtp.sina.com";
//用户名凭证
mail.Credentials = new System.Net.NetworkCredential("mail_user_name", "mail_user_password");
//邮件信息
MailMessage message = new MailMessage();
//发件人
message.From = new MailAddress("mail_user_name@sina.com");
//收件人
foreach (object item in MailTo)
{
message.To.Add(item.ToString());
}
//主题
message.Subject = Subject;
//内容
message.Body = Body;
//正文编码
message.BodyEncoding = System.Text.Encoding.UTF8;
//设置为HTML格式
message.IsBodyHtml = true;
//优先级
message.Priority = MailPriority.High;
try
{
mail.Send(message);
result = "发送成功";
return result;
}
catch (Exception e)
{
result = e.ToString();
}
return result;
}
不同的邮箱,要改的部分主要是“上面代码中高亮的部分”,有些邮箱地址是要加后缀“@xxx.com”,而有些邮箱只要用户名就好了 .
如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.163.com");
smtp.Credentials = new System.Net.NetworkCredential("163emailaddress","emailpassword");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("163emailaddress@163.com");
附带以前2003版的:
/**//// <summary>
/// 发送邮件
/// </summary>
/// <param name="Sender">发件人邮箱地址</param>
/// <param name="Receiver">收件人邮箱地址</param>
/// <param name="Title">邮件标题</param>
/// <param name="Content">邮件内容</param>
/// <param name="SmtpServer">邮箱服务器名称</param>
/// <param name="Validate">服务器是否需要验证</param>
/// <param name="UserName">验证用户名</param>
/// <param name="Password">验证密码</param>
/// <returns>结果</returns>
public string SendMail(string from,string to,string title,string body,string enclosure,
string smtpServer,bool validate,string userName,string password)
{
MailMessage mail = new MailMessage();//实例化MailMessage
mail.From = from; //邮件发件人
mail.To = to;//邮件收件人
mail.Subject = title;//邮件标题
mail.Body = body; //邮件内容
mail.BodyFormat = MailFormat.Html; //邮件文本格式
mail.Priority = MailPriority.Normal;//邮件发送级别
if(enclosure != "")
{
//添加附件
MailAttachment ma = new MailAttachment(enclosure);
mail.Attachments.Add(ma);
}
//邮件服务器是否需要验证
if(validate)
{
if(userName != "" && password != "")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
else
{
return "缺少验证用户名和密码";
}
}
try
{
SmtpMail.SmtpServer = smtpServer;//设置邮件服务器
//发送邮件
SmtpMail.Send(mail);
return "邮件发送成功!";
}
catch
{
return "邮件发送失败!";
}
}
- smtpClient发邮件错误:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- smtpClient发邮件错误:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- Send Error:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- Send Error:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- Send Error:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- 163邮箱发送邮件碰到:System.Net.Mail.SmtpException: 不允许使用邮箱名称。 服务器响应为:authentication is required
- 不允许使用邮箱名称。 服务器响应为:authentication is required
- smtpClient发邮件错误:不允许使用邮箱名称
- .Net使用SmtpClient发送邮件,报错:不允许使用邮箱名称。 服务器响应为: xxxw
- JavaMail 553 You are not authorized to send mail
- 让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized
- [转]让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized
- HTTP Error 401.2 - You are not authorized to view this page due to invalid authentication headers
- you are not authorized to use service
- 解决使用SmtpClient类时出现"不允许使用邮箱名称"的错误
- The IP you're using to send email is not authorized
- 登录tomcat 时出现错误:tomcat You are not authorized to view this page
- you are not authorized to use this service
- ASP.NET 递归 生成树 TreeView
- Oracle多行转换成字符串方法总结
- Jbuilder2006使用JDBC驱动连接SQL2000数据库
- Framework2.0泛型约束
- 如何安装SQL2000的sp3补丁
- smtpClient发邮件错误:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
- javax.servlet.jsp.JspException: Missing message for key "xxxxxx"
- 强大的社工之-重庆黑客联盟
- No getter method for property suggested of bean org.apache.struts.taglib...解决方法
- Cannot retrieve mapping for action /InquiryAction解决方法
- Enterprise Architect使用心德
- You must specify exactly one of "forward", "href", "page" or "action
- java.lang.ClassCastException
- JDK6.0学习笔记(二)垃圾收集机制-对象引用的类型