首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

asp.net发送电子邮件有关问题?

2012-07-28 
asp.net发送电子邮件问题?????MailMessage mailObj new MailMessage()mailObj.From new MailAddress(

asp.net发送电子邮件问题?????
MailMessage mailObj = new MailMessage();
  mailObj.From = new MailAddress("lz6e804@lisco.com.cn");//发信人地址
  mailObj.To.Add("haoyuning@foxmail.com");//收信地址
  mailObj.CC.Add("haoyuning@foxmail.com");//抄送地址
  mailObj.Subject = "这里填写邮件标题2222222";//邮件标题
  mailObj.Body = "这里是正文的内容2222222222222222222222";
  //html格式的邮件
  mailObj.IsBodyHtml = true;
  ////设置为高级优先权(可设可不设)
  //mailObj.Priority = MailPriority.High;
  //使用SmtpMail对象发送邮件
  SmtpClient smtp = new SmtpClient("123.156.97.148");//服务器的地址
  smtp.Send(mailObj);//发送邮件

运行以上代码(报错):由于目标机器积极拒绝,无法连接。 123.156.97.148:25 
怎么解决?????


[解决办法]
是在什么环境下出现这种情况的,我之前遇到过类似的情况,但是是本机OK,放到服务器上就不行,原因是防火墙把25的端口关了
[解决办法]
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="subject">邮件标题</param>
/// <param name="content">邮件内容</param>
/// <param name="tomail">接收邮件地址</param>
protected void SendMail(string subject, string content, string tomail)
{
MailMessage objMailMessage;

// 创建邮件消息 
objMailMessage = new MailMessage();
objMailMessage.From = rwWebConfig.readConfigValue("formemail");
objMailMessage.To = tomail;
objMailMessage.Subject = subject; 
objMailMessage.BodyFormat = MailFormat.Html;
objMailMessage.Body = content;

objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名 
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", rwWebConfig.readConfigValue("username"));
//密码 
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", rwWebConfig.readConfigValue("password"));

//SMTP地址 
SmtpMail.SmtpServer = rwWebConfig.readConfigValue("smtp");
//开始发送邮件 
SmtpMail.Send(objMailMessage);
}
[解决办法]
参考这里
http://www.heycoder.com/noteinfo8.aspx
[解决办法]
1.QQ: 

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add("xxx@xxx.com");
msg.From = new MailAddress("xxxxx@qq.com", "handyzhu", System.Text.Encoding.UTF8);
msg.Subject = "标题";
msg.Body = "测试";
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("164206528@qq.com","********");
client.Port = 25;
client.Host = "smtp.qq.com";
object userState = msg;

client.Send(msg);

 

2.163

 

public static string CreateTimeoutTestMessage(string server) 

string Success = "发送成功"; 
try 

string _to = "xxxx@qq.com"; 
string _from = "xxx@163.com"; 
string _subject = "Using the new SMTP client."; 
string _body = @"Using this new feature, you can send an e-mail message from an application very easily."; 


MailMessage message = new MailMessage(); 
message.From = new MailAddress(_from); 
//可以利用MailMessage.To.Add方法增加要发送的邮件地址
message .To .Add (new MailAddress ("xxxx@qq.com")); 
message.To.Add(new MailAddress(_to)); 
message.Subject = _subject; 
message.Body = _body; 
 
//添加附件
Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar"); 
message.Attachments.Add(a); 
//设置邮箱的地址或IP
SmtpClient client = new SmtpClient(server); 
//设置邮箱端口,pop3端口:110, smtp端口是:25
client.Port = 25; 

//设置超时时间
client.Timeout = 9999; 

//要输入邮箱用户名与密码

client.Credentials = new NetworkCredential("xxxx@163.com", "******"); 
client.Send(message); 

catch (Exception ex) 

Success = ex.ToString(); 

return Success; 
}
[解决办法]
///
/// 发送EMAIL
/// 
/// </summary>
///
/// <param name="from">发件人地址 </param>
///
/// <param name="password">密码 </param>
///
/// <param name="DisPalyName">对方收到时显示的名称 </param>
///
/// <param name="to">收件人 </param>
/// <param name="cc">抄送:默认为空 </param>
///
/// <param name="subject">主题 </param>
///
/// <param name="ssl">经过ssl加密 </param>
///
/// <param name="priority">邮件优先级:MailPriority.High </param> 
///
/// <param name="isbodyhtml">是否HTML邮件 </param>
///
/// <param name="attachment">附件:没有则为null </param>
///
/// <param name="body">正文 </param>
/// <returns>返回值:如果为空则成功,否则失败 </returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, bool ssl, MailPriority priority, bool isbodyhtml, Attachment attachment, string body)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from, DisPalyName, Encoding.UTF8);
mm.To.Add(to);
mm.SubjectEncoding = Encoding.UTF8;
mm.Subject = subject;
if (attachment != null)
mm.Attachments.Add(attachment);
if (cc != null && cc != "")
mm.CC.Add(cc);
//抄送
mm.IsBodyHtml = isbodyhtml;
//是否是HTML邮件
mm.BodyEncoding = Encoding.UTF8;
//编码
mm.Priority = priority;
// MailPriority.High; //优先级
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Credentials = new System.Net.NetworkCredential(from, password);
//
sc.Port = 25;
sc.Host = "smtp." + from.Substring(from.IndexOf('@') + 1);
try
{
sc.Send(mm);
return "";
}
catch (Exception ee)
{
return ee.ToString();
}
}
/// <summary>
///


/// 发送EMAIL
/// </summary>
/// <param name="from">发件人地址 </param>
///
/// <param name="password">密码 </param>
///
/// <param name="DisPalyName">对方收到时显示的名称 </param> 
///
/// <param name="to">收件人 </param>
///
/// <param name="cc">抄送:默认为空 </param>
///
/// <param name="subject">主题 </param>
///
/// <param name="attachment">附件 </param>
///
/// <param name="body">正文 </param>
///
/// <returns>返回值:如果为空则成功,否则失败 </returns>
///
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, Attachment attachment, string body)
{
return SendEMail(from, password, DisPalyName, to, cc, subject, false, MailPriority.High, false, attachment, body);
}

热点排行