读书人

.net2.0发送邮件有关问题

发布时间: 2012-01-08 22:48:50 作者: rapoo

.net2.0发送邮件问题
配置文件里我这么写的
<system.net>
<mailSettings>
<smtp from= "tss19841011@sina.com ">
<network port= "25 " userName= "我的邮箱用户名 " password= "我的邮箱密码 " defaultCredentials= "true " host= "smtp.sina.com "/>
</smtp>
</mailSettings>
</system.net>

这是事件代码
MailMessage m_message = new MailMessage();

m_message.From = new MailAddress( "tss19841011@sina.com ");

m_message.To.Add(new MailAddress( "tss19841011@sina.com "));

m_message.Subject = "test ";

m_message.Body = "hello ";

SmtpClient m_smtpClient = new SmtpClient();

m_smtpClient.Send(m_message);
试了很多方法都报错...求高手解决 或者提供点别的出路 实在感激不尽 100分送上

[解决办法]
public Boolean sendemail()
{
_fromadr = "tss19841011@163.com ";
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(_fromadr,_sendadr,_title,_content);
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.Default;
if (_bcadr != null)
{
message.CC.Add(_bcadr);
}
if (_filename != null)
{
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(_filename, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(_filename);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(_filename);
disposition.ReadDate = System.IO.File.GetLastAccessTime(_filename);
message.Attachments.Add(data);
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( "smtp.163.com ");
client.Credentials = new NetworkCredential( "tss19841011 ", "*password* ");//这个要你的163的用户名和密码
try
{
client.Send(message);
return true;
}
catch (Exception ex)
{
_errormsg = ex.Message;
return false;
}

}

补充说一下,我式过sina的服务器不行,163.com的可以发~
[解决办法]
/// <summary>
/// 发送邮件
/// </summary>
/// <param name= "strSmtpServer "> smtp地址 </param>
/// <param name= "UserName "> 用户名 </param>
/// <param name= "Password "> 密码 </param>
/// <param name= "strFrom "> 发信人地址 </param>
/// <param name= "strto "> 收信人地址 </param>


/// <param name= "strSubject "> 邮件标题 </param>
/// <param name= "strBody "> 邮件正文 </param>
public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);

//表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true;



//包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password);

//指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。

client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

//建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject,strBody);

//定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");
message.SubjectEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");

//获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = false;

//指定邮件优先级

message.Priority = System.Net.Mail.MailPriority.Normal;

//添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");
if (strFileName != " " && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}


//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password)


//发送
client.Send(message);
}


===================================

vs2005 调试通过
[解决办法]
/*****************************************/
/* Project name: MailDaemon */
/* Module name: Mail Sender */
/* Author: Ming Yeh */
/* Created date: 2006-08-21 */
/* Last modified by: */
/* Last modify date: */
/* .-._ */
/* {_}^ )o */
/* {\________//~` */
/* ( ) */
/* /||~~~~~||\ */
/* |_\\_ \\_\_ */
/* */
/*****************************************/
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace mailDaemon
{
class MailSender
{
private const string SERVER_IP = "61.181.255.80 ";
private const string SENDER_EMAIL = "mailist@gaosulu.com ";
private const string USERNAME = "mailist ";
private const string PASSWORD = "bu2kwh1at ";
private const string SUBJECT = "高速路商机速递 ";


string _server;
string _senderEmail;
string _username;
string _password;
static bool mailSent = false;

public MailSender(string server, string senderEmail, string username, string password)
{
_server = server;
_senderEmail = senderEmail;
_username = username;
_password = password;
}
public MailSender():this(SERVER_IP, SENDER_EMAIL, USERNAME, PASSWORD)
{

}
public void SendMail(string email, string content)
{
MailMessage msg = new MailMessage(_senderEmail, email, SUBJECT, content);
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = MailPriority.High;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient(_server);
NetworkCredential credential = new NetworkCredential(_username, _password);
client.UseDefaultCredentials = false;
client.Credentials = credential;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(msg, "Message Sent. ");
Console.WriteLine(email + "邮件发送中,按C取消发送 ");
//string answer = Console.ReadLine();
//if (answer.StartsWith( "c ") && mailSent == false)
//{
// client.SendAsyncCancel();
//}
Console.WriteLine(email + "处理完成 ");
}

void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
String token = (string)e.UserState;

if (e.Cancelled)
{
Console.WriteLine( "[{0}] Send canceled. ", token);
}
if (e.Error != null)
{
Console.WriteLine( "[{0}] {1} ", token, e.Error.ToString());
}
else
{
Console.WriteLine( "Message sent. ");
}
mailSent = true;
}
}
}

[解决办法]
private void SendMail(string __SMTPServerName,string __SMTPUser,string __SMTPPassword,
string __from,string __recipient,string __title,string __body)
{

MailMessage eMail = new MailMessage();
eMail.BodyFormat = MailFormat.Html;
eMail.Subject=__title;
eMail.Body = __body;
eMail.From = __from;
StringBuilder MailBody = new StringBuilder();
MailBody.Append( " <style> A:visited {TEXT-DECORATION: none} ");
MailBody.Append( "A:active {TEXT-DECORATION: none} ");
MailBody.Append( "A:hover {TEXT-DECORATION: underline} ");
MailBody.Append( "A:link {text-decoration: none;} ");
MailBody.Append( "A:visited {text-decoration: none;} ");
MailBody.Append( "BODY {FONT-FAMILY: Verdana,宋体; FONT-SIZE: 9pt;} ");
MailBody.Append( "TD {FONT-FAMILY: Verdana,宋体; FONT-SIZE: 9pt} </style> ");
MailBody.Append( " <TABLE border=0 width= '95% ' align=center> <TBODY> <TR> ");
MailBody.Append( " <TD valign=middle align=top> ");
MailBody.Append( " <p> " + __body+ " <p> ");
MailBody.Append( " </TD> </TR> ");
MailBody.Append( " </TBODY> </TABLE> <br> ");



eMail.Body = MailBody.ToString();
eMail.Fields[ "http://schemas.microsoft.com/cdo/configuration/smtsperver "]
= __SMTPServerName;
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpserverport "] = 25;
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusing "] = 2;
if (__SMTPUser != null &&__SMTPUser.Trim()!= " ")
{
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate "] = 1;
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusername "] =
__SMTPUser;
}
if( __SMTPPassword != null&&__SMTPPassword.Trim()!= " ")
{
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendpassword "] =
__SMTPPassword;
}
if (__SMTPUser != null &&__SMTPUser.Trim()!= " "&& __SMTPPassword != null&&__SMTPPassword.Trim()!= " ")
{
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate "] = 1;
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusername "] =
__SMTPUser;
eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendpassword "] =
__SMTPPassword;
}
eMail.To = __recipient;
SmtpMail.SmtpServer = __SMTPServerName;
SmtpMail.Send(eMail);
}

读书人网 >asp.net

热点推荐