邮件发送失败的信息:.NET 发送邮件失败。 在邮件标头中找到无效的字符:“周”。
亲,一大早就给我来了个邮件发送失败的东西!
使用的是
using System.Net.Mail;
下的邮件发送类,很郁闷,网上查了资料。叽叽歪歪的,也都没有解决。
http://topic.csdn.net/u/20080327/14/753ef8e2-ed87-4d8d-a6ab-41ba9b8f49f5.html
http://hi.baidu.com/dostruggle/item/d416df746626ff2d5c1789a5
有人总结了三种说法如下:
第一种,是在 SmtpClient.Send 之前曾经调用过该 MailMessage 对象的 From, To, Cc 等字段的 ToString 方法。很有可能的情形是,你尝试在发信前留下日志时,“无意间”调用到了。而微软的工程师在此处出现了一些失误,最终产生了该错误消息,具体情况请参看链接中的文章。
http://columns.chicken-house.net/blogs/chicken/archive/2007/04/06/system-net-mail-bug.aspx
第二种,微软知识库里给出的一种原因是因为收件人显示名称中包含有引号。
第三种,你的计算机机名称中有汉字。原因是SmtpClient使用了计算机名作为smtp协议中HELO命令的参数,而在发送命令时又会对字符进行检查,若是非法字符则会抛出异常,很不幸中文字符都包括在内。
根据本人亲自试验,证实第三种说法是正确的,将计算机名称改成非汉字就可以正确发送了。可能在别的运行环境中有些不同。我的环境是:WinXP+SP3,.net3.5。要做这方面的软件的话,有这个Bug还真是不好用,总不能要别人改计算机名吧。
但是,我的计算机名字本身就不是汉字的,这让我情何以堪啊!
后来,只能使用别的类试试看了。
using System.Web.Mail;
下的邮件发送机制竟然发送成功了!代码如下
protected void SendMail(string job, string sendName, string contact, HttpPostedFile httpFile)
{
string smtpServer = "smtp.163.com"; //smtp.exmail.qq.com 发送邮件服务器
string strSend = "xx@163.com"; //帐号
string strPass = ""; //密码
string strRecive = "xx"; //接收者密码
string strTitle = sendName + " 简历";
string strBody = string.Format("申请职位: {0} 姓名: {1} 联系方式:{2}", job, sendName, contact);
#region 上传文件
string strFolder = Server.MapPath("~/join/files/");
if (!Directory.Exists(strFolder)) Directory.CreateDirectory(strFolder); //如果目录不存在,则创建
string fileName = httpFile.FileName.Substring(httpFile.FileName.LastIndexOf('\\') + 1);
fileName = fileName.Substring(0, fileName.IndexOf('.'));
string strFileName = fileName + "--" + DateTime.Now.ToString("yyyyMMddHHmm");
string extName = Path.GetExtension(httpFile.FileName).ToLower(); //取出文件后缀名
string pathFileName = strFolder + strFileName + extName; //拼接文件名
httpFile.SaveAs(pathFileName);
#endregion
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建一个附件对象
objMailAttachment = new MailAttachment(pathFileName);//发送邮件的附件
// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = strSend;//源邮件地址
objMailMessage.To = strRecive;//目的邮件地址
objMailMessage.Subject = strTitle;//发送邮件的标题
objMailMessage.Body = strBody;//发送邮件的内容
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", strSend);
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPass);
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为:530 Authentication required
//SMTP地址
SmtpMail.SmtpServer = smtpServer;
// 开始发送邮件
// 在发送之前,去新浪邮箱里开启POP/SMTP设置 邮箱设置->账户->POP/SMTP设置->开启
// 否则会报错误0x80040217. The server response was not available
string msg = "";
try
{
SmtpMail.Send(objMailMessage);
}
catch (Exception ex)
{
msg = "发送失败,请稍候重试!"; //发送失败,请稍候重试!
}
finally
{
if (string.IsNullOrEmpty(msg)) msg = "发送成功!";
Response.Write(string.Format("<script>alert('{0}');</script>", msg));
File.Delete(pathFileName); //删除附件
}
}
附上第一种邮件发送的代码,这代码我之前使用的时候是正常的!可能是环境配置的原因吧!
博客原文:http://blog.csdn.net/andrew_wx/article/details/6950555
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
namespace SendMailExample
{
/// <summary>
/// 作者:Andrew
/// Blog: http://blog.csdn.net/Andrew_wx
/// </summary>
public partial class FormSendMail : Form
{
public FormSendMail()
{
InitializeComponent();
}
private void FormSendMail_Load(object sender, EventArgs e)
{
txtSmtpServer.Text = "smtp.qq.com";
txtSend.Text = "heuandmei@qq.com";
txtDisplayName.Text = "Andrew(王旭)";
txtPassword.Text = "";//密码
txtReceive.Text = "heuandmei@qq.com";
txtTitle.Text = "发信测试";
txtBody.Text = "This is a test(测试)";
rbtnNoSSL.Checked = true;
}
private void btnAddFiles_Click(object sender, EventArgs e)
{
OpenFileDialog odlg = new OpenFileDialog();
odlg.CheckFileExists = true;
//只接收有效的文件名
odlg.ValidateNames = true;
//允许一次选择多个文件作为附件
odlg.Multiselect = true;
if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lstFiles.Items.AddRange(odlg.FileNames);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(
txtSend.Text, txtDisplayName.Text, Encoding.UTF8);
mail.To.Add(txtReceive.Text);
mail.Subject = txtTitle.Text;
mail.SubjectEncoding = Encoding.Default;
mail.Body = txtBody.Text;
mail.BodyEncoding = Encoding.Default;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.Normal;
//添加附件
Attachment attachment = null;
if (lstFiles.Items.Count > 0)
{
for (int i = 0; i < lstFiles.Items.Count; i++)
{
string pathFileName = lstFiles.Items[i].ToString();
string extName = Path.GetExtension(pathFileName).ToLower();
//判断附件类型
if (extName == ".rar" || extName == ".zip")
{
attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip);
}
else
{
attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet);
}
ContentDisposition cd = attachment.ContentDisposition;
cd.CreationDate = File.GetCreationTime(pathFileName);
cd.ModificationDate = File.GetLastWriteTime(pathFileName);
cd.ReadDate = File.GetLastAccessTime(pathFileName);
mail.Attachments.Add(attachment);
}
}
SmtpClient client = new SmtpClient();
client.Host = txtSmtpServer.Text;
client.Port = 25;
//是否使用安全套接字层加密连接
client.EnableSsl = rbtnUseSSL.Checked;
//不使用默认凭证,注意此句必须放在 client.Credentials 的上面
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(txtSend.Text, txtPassword.Text);
//邮件通过网络直接发送到服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(mail);
MessageBox.Show("发送成功");
}
catch (SmtpException ex)
{
MessageBox.Show("发送失败:" + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("发送失败:" + ex.Message);
}
finally
{
mail.Dispose();
client = null;
this.Cursor = Cursors.Default;
}
}
}
}