using System.Net.Mail;
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
string SendName = TextBox1.Text;//"[email protected]";//收件人地址
//string SendCName = "[email protected]";//抄送人地址
msg.To.Add(SendName);
//msg.CC.Add(SendCName); //抄送收件人地址
msg.From = new MailAddress(SendName, null, System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("[email protected]", "123456789abcd");//上述写你的GMail邮箱和密码
client.Port = 587;//Gmail使用的端口
client.Host = "smtp.gmail.com";
client.EnableSsl = true;//经过ssl加密
object userState = msg;
try
{
//client.SendAsync(msg, userState);//用这个发不出去 client.Send(msg);//用这个只能给163发 给其他的发报错
Response.Write("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
我不想用JMAIL .NET提供的类难道就不能发送邮件吗?? 哪个朋友做过给贴点代码上来小弟在这里谢谢了...
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
string SendName = TextBox1.Text;//"[email protected]";//收件人地址
//string SendCName = "[email protected]";//抄送人地址
msg.To.Add(SendName);
//msg.CC.Add(SendCName); //抄送收件人地址
msg.From = new MailAddress(SendName, null, System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("[email protected]", "123456789abcd");//上述写你的GMail邮箱和密码
client.Port = 587;//Gmail使用的端口
client.Host = "smtp.gmail.com";
client.EnableSsl = true;//经过ssl加密
object userState = msg;
try
{
//client.SendAsync(msg, userState);//用这个发不出去 client.Send(msg);//用这个只能给163发 给其他的发报错
Response.Write("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
我不想用JMAIL .NET提供的类难道就不能发送邮件吗?? 哪个朋友做过给贴点代码上来小弟在这里谢谢了...
using System.Collections.Generic;
using System.Text;using System.Net.Mail;namespace SmtpMail
{
class Program
{
/// <summary>
/// Console App to send mails via Smtp
/// </summary>
/// <param name="args[0]">From</param>
/// <param name="args[1]">To</param>
/// <param name="args[2]">Subject</param>
/// <param name="args[3]">Body</param>
/// <param name="args[4]">Host</param>
/// <param name="args[5]">Port</param>
/// <param name="args[6]">User</param>
/// <param name="args[7]">Password</param>
static void Main(string[] args)
{
try
{
// TODO: Add error handling for invalid arguments // To
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(args[1]); // From
MailAddress mailAddress = new MailAddress(args[0]);
mailMsg.From = mailAddress; // Subject and Body
mailMsg.Subject = args[2];
mailMsg.Body = args[3]; // Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient(args[4], Convert.ToInt32(args[5]));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(args[5], args[5]);
smtpClient.Credentials = credentials; smtpClient.Send(mailMsg);
}
catch (Exception ex)
{
Console.WriteLine( ex.Message );
} }
}
}
添加 Interop.CDO.dll,adodb 引用
public static void CDOsendmail(string from, string to, string subject,
string body, string userName, string password, string smtpServer)
{
//声明新的邮件实例
CDO.Message Msg = new CDO.Message();
//分别设置发送人、收信人、主题、内容
Msg.From = from;
Msg.To = to;
Msg.Subject = subject;
Msg.HTMLBody = "<html><body>"+body
+"</body></html>";
//设置发送参数,包括smtpServer,用户名,密码
CDO.IConfiguration Config = Msg.Configuration;
ADODB.Fields oFields = Config.Fields;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value=userName;
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value=password;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value=smtpServer;
oFields.Update();
//字符格式
Msg.BodyPart.Charset = "gb2312";
Msg.HTMLBodyPart.Charset = "gb2312";
//发送
Msg.Send();
Msg = null;
}
你可以用JMAIL来发送试试。
jmail.MessageClass ms = new jmail.MessageClass();
ms.Charset = "gb2312";
ms.ContentType = "text/html";
ms.ISOEncodeHeaders = false;
ms.Priority = Convert.ToByte(1);
ms.From = "[email protected]";
ms.FromName = "Lee";
ms.Subject = "瑞润测试信息";
ms.MailServerUserName = "邮箱的用户名";
ms.MailServerPassWord = "邮箱的密码";
ms.AddRecipient("[email protected]", "", "");
ms.Body = "发送一个微笑!!:)";
if (ms.Send("smtp.qq.com", false))
{
Label1.Text = "发送成功!";
}
else
{
Label1.Text = "发送失败!!请重试!";
}
jmail可以在网络上下载下来。你自己下就可以了!或者你给我留个邮箱我发给你也可以!
.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 393px">
收信:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
主题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" style="width: 400px">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,发表成功!"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" /></td>
</tr>
</table>
</form>
</body>
</html>
.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class sendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////设置发件人信箱,及显示名字
MailAddress from = new MailAddress("[email protected]", "J.L.C");
//设置收件人信箱,及显示名字
MailAddress to = new MailAddress(TextBox1.Text, "JLC");
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); oMail.Subject = TextBox2.Text; //邮件标题
oMail.Body = TextBox3.Text; //邮件内容 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高 //发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.126.com"; //指定邮件服务器
client.Credentials = new NetworkCredential("[email protected]", "jlc3509589934");//指定服务器邮件,及密码 //发送
try
{
client.Send(oMail); //发送邮件
Label1.Text = "恭喜你!邮件发送成功。";
}
catch
{
Label1.Text = "邮件发送失败,检查网络及信箱是否可用。";
} oMail.Dispose(); //释放资源 TABLE1.Visible = false;
Table2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
//返回,继续发送
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
}
///
/// </summary>
/// <param name="from">发送邮件地址</param>
/// <param name="to">接收邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="name">用户名</param>
/// <param name="pwd">用户密码</param>
/// <param name="server">smtp服务器</param>
public void sendMail(string from,string to,string subject,string body,string name,string pwd,string server)
{
try
{
System.Web.Mail.MailMessage myMail=new MailMessage();
myMail.From =from;
myMail.To =to;
myMail.Subject =subject;
myMail.Priority = MailPriority.Low;
myMail.BodyFormat = MailFormat.Text;
myMail.Body =body;
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", name); //set your username here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd); //set your password here
SmtpMail.SmtpServer=server;//your smtp server here
SmtpMail.Send(myMail);
Response.Write("<script>alert('发送成功!')</script>");
}
catch(Exception e)
{
throw e;
}
} 如果是2.0,代码如下: /// <summary>
///
/// </summary>
/// <param name="from">发送邮件地址</param>
/// <param name="to">接收邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="name">用户名</param>
/// <param name="pwd">用户密码</param>
/// <param name="server">smtp服务器</param>
public void SendMail(string from, string to, string subject, string body, string name, string pwd, string server)
{
MailMessage mess = new MailMessage();
mess.From = new MailAddress(from);
mess.Subject = subject;
mess.IsBodyHtml = true;
mess.BodyEncoding = System.Text.Encoding.UTF8;
mess.Body = body;
SmtpClient client = new SmtpClient();
client.Host =server;
client.Credentials = new System.Net.NetworkCredential(name, pwd);
mess.To.Add(new MailAddress(to)); try
{
client.Send(mess);
Response.Write("邮件发送到" + mess.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.Message + "<br>");
}
}
错误:
//SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为: 5.7.0 Must issue a STARTTLS command first. i6sm12257614tid.36"} System.Exception {System.Net.Mail.SmtpException}
////设置发件人信箱,及显示名字
MailAddress from = new MailAddress("[email protected]");
//设置收件人信箱,及显示名字
MailAddress to = new MailAddress(TextBox1.Text);
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); oMail.Subject = TextBox2.Text; //邮件标题
oMail.Body = TextBox3.Text; //邮件内容 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高 //发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com"; //指定邮件服务器
client.Credentials = new NetworkCredential("[email protected]", "123456789abcd");//指定服务器邮件,及密码 //发送
try
{
client.Send(oMail); //发送邮件
Label1.Text = "恭喜你!邮件发送成功。";
}
catch(Exception ex)
{
Label1.Text = ex.ToString();
} oMail.Dispose();
SendMail("[email protected]", TextBox1.Text, "测试", "主题", "[email protected]", "123456789abcd", "smtp.gmail.com");SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为: 5.7.0 Must issue a STARTTLS command first. d1sm10863699tid.4这都是怎么回事啊??我怎么就发不出去有加啊? 需要配置什么地方吗?
我按照4楼的大哥的做法来执行,可是提示发送失败哦 ,请指教:)
smtp需要身份认证,具体操作方法参考:http://www.cnitblog.com/yhf119/archive/2006/02/18/6691.html
jmail.MessageClass ms = new jmail.MessageClass();
ms.Charset = "gb2312";
ms.ContentType = "text/html";
ms.ISOEncodeHeaders = false;
ms.Priority = Convert.ToByte(1);
ms.From = "[email protected]";
ms.FromName = "zjy";
ms.Subject = "测试信息";
ms.MailServerUserName = "[email protected]";
ms.MailServerPassWord = "123456789abcd";
ms.AddRecipient("[email protected]", "", "");
ms.Body = "发送一个微笑!!:)";
if (ms.Send("smtp.gmail.com", false))
{
TextBox1.Text = "发送成功!";
}
else
{
TextBox1.Text = "发送失败!!请重试!";
}
MailMessage mail = new MailMessage();
mail.To = "[email protected]";
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.BodyFormat = MailFormat.Html;//设置为HTML格式
//设置为需要用户验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置验证用户名(把my_username_here改为你的验证用户名)
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "zjy.company");
//设置验证密码(把password改为你的验证密码)
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456789abcd");
SmtpMail.SmtpServer = "mail.gmail.com"; //邮件服务器地址
try
{
SmtpMail.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
{
try
{
MailAddress sendAddress = new MailAddress(txtEmail1.Text.Trim());
MailAddress toAddress = new MailAddress(txtEmail2.Text.Trim());
MailMessage message = new MailMessage(sendAddress, toAddress);
message.Subject = txtTitle.Text;
message.IsBodyHtml = true;
message.Body = txtContent.Text;
string smtp = "邮件服务器";
SmtpClient sc = new SmtpClient(smtp);
sc.Credentials = new System.Net.NetworkCredential(txtEmail1.Text.Trim(), "邮箱密码");
sc.Send(message);
}
catch (SmtpException ex)
{
throw new System.Exception(ex.ToString());
}
}
smtp端口改为465
System.Net.Mail.SmtpException: 操作已超时。 在 System.Net.Mail.SmtpClient.Send(MailMessage message) 在 admin_Course_courseSentEmail.Button1_Click(Object sender, EventArgs e) 位置 e:\工作目录\OfficeChannel\OfficeChannel\ad_con_data\Course\courseSentEmail.aspx.cs:行号 52
郁闷的是我们公司的邮件服务器默认465也不行