smtp 服务器要求安全连接或客户端未通过身份验证,该怎么处理
smtp 服务器要求安全连接或客户端未通过身份验证
最近在写一个发邮件的小程序,一直报上面的错,大家给我看原因,代码如下:
前端主要代码代码:
<form id=form1 runat=server >
<tr>
<td >
收件人邮箱地址:
</td>
<td >
<asp:TextBox ID="tb_Email" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
抄送至:
</td>
<td >
<asp:TextBox ID="tb_cc" runat="server" Width="268px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
邮件主题:
</td>
<td >
<asp:TextBox ID="tb_Subject" runat="server" Width="268px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
邮件内容:
</td>
<td >
<asp:TextBox ID="tb_Body" runat="server" Height="63px" TextMode="MultiLine" Width="266px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
添加附件:
</td>
<td >
<asp:FileUpload ID="tb_Attachment" runat="server" Width="265px" />
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="btn_SendEmail" runat="server" Text="发送邮件" OnClick="btn_SendEmail_Click" />
</td>
</tr>
</form>
后端主要代码:
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void btn_SendEmail_Click(object sender, EventArgs e)
{
//声明一个Mail对象
MailMessage mymail = new MailMessage();
//发件人地址
//如是自己,在此输入自己的邮箱
mymail.From = new MailAddress("xxxx@.com"); //我自己填的是outlook账号!
//收件人地址
mymail.To.Add(new MailAddress(tb_Email.Text));
//邮件主题
mymail.Subject = tb_Subject.Text;
//邮件标题编码
mymail.SubjectEncoding = System.Text.Encoding.UTF8;
//发送邮件的内容
mymail.Body = tb_Body.Text;
//邮件内容编码
mymail.BodyEncoding = System.Text.Encoding.UTF8;
//添加附件
Attachment myfiles = new Attachment(tb_Attachment.PostedFile.FileName);
mymail.Attachments.Add(myfiles);
//抄送到其他邮箱
mymail.CC.Add(new MailAddress(tb_cc.Text));
//是否是HTML邮件
mymail.IsBodyHtml = true;
//邮件优先级
mymail.Priority = MailPriority.High;
//创建一个邮件服务器类
SmtpClient myclient = new SmtpClient();
myclient.Host = "smtp.outlook.com";
//SMTP服务端口
myclient.Port = 25;
//验证登录
myclient.Credentials = new System.Net.NetworkCredential("xxxx@", "*******");//这里是我自己的outlook账号和密码。
myclient.Send(mymail); //vs下会在这行报上面那个错。
}
}
------解决思路----------------------
outlook没试过,要不要设置开启stmp服务,这是我的可用的,用的网易126
string from = txtFrom.Text;
string fromname = txtDisplayName.Text;
string subject = txtEmailTitle.Text;
string body = txtBody.Text;
string username = txtFrom.Text;
string password = txtPasswordFrom.Text;
string server = txtSmtp.Text;
int port = int.Parse(txtPort.Text);
//邮件发送类
MailMessage mail = new MailMessage();
//是谁发送的邮件
mail.From = new MailAddress(from, fromname);
//发送给谁
for (int i = 0; i < to.Length; ++i)
{
mail.To.Add(to[i]);
}
//标题
mail.Subject = subject;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = true;
//附件
//selectPath = AppDomain.CurrentDomain.BaseDirectory + selectPath;
if (!string.IsNullOrWhiteSpace(FilePath))
{
mail.Attachments.Add(new Attachment(FilePath));
}
//邮件服务器和端口
SmtpClient smtp = new SmtpClient(server,port);
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(username, password);
//超时时间
smtp.Timeout = 30000;
smtp.Send(mail);
return "ok";
最近在写一个发邮件的小程序,一直报上面的错,大家给我看原因,代码如下:
前端主要代码代码:
<form id=form1 runat=server >
<tr>
<td >
收件人邮箱地址:
</td>
<td >
<asp:TextBox ID="tb_Email" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
抄送至:
</td>
<td >
<asp:TextBox ID="tb_cc" runat="server" Width="268px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
邮件主题:
</td>
<td >
<asp:TextBox ID="tb_Subject" runat="server" Width="268px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
邮件内容:
</td>
<td >
<asp:TextBox ID="tb_Body" runat="server" Height="63px" TextMode="MultiLine" Width="266px"></asp:TextBox>
</td>
</tr>
<tr>
<td >
添加附件:
</td>
<td >
<asp:FileUpload ID="tb_Attachment" runat="server" Width="265px" />
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="btn_SendEmail" runat="server" Text="发送邮件" OnClick="btn_SendEmail_Click" />
</td>
</tr>
</form>
后端主要代码:
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void btn_SendEmail_Click(object sender, EventArgs e)
{
//声明一个Mail对象
MailMessage mymail = new MailMessage();
//发件人地址
//如是自己,在此输入自己的邮箱
mymail.From = new MailAddress("xxxx@.com"); //我自己填的是outlook账号!
//收件人地址
mymail.To.Add(new MailAddress(tb_Email.Text));
//邮件主题
mymail.Subject = tb_Subject.Text;
//邮件标题编码
mymail.SubjectEncoding = System.Text.Encoding.UTF8;
//发送邮件的内容
mymail.Body = tb_Body.Text;
//邮件内容编码
mymail.BodyEncoding = System.Text.Encoding.UTF8;
//添加附件
Attachment myfiles = new Attachment(tb_Attachment.PostedFile.FileName);
mymail.Attachments.Add(myfiles);
//抄送到其他邮箱
mymail.CC.Add(new MailAddress(tb_cc.Text));
//是否是HTML邮件
mymail.IsBodyHtml = true;
//邮件优先级
mymail.Priority = MailPriority.High;
//创建一个邮件服务器类
SmtpClient myclient = new SmtpClient();
myclient.Host = "smtp.outlook.com";
//SMTP服务端口
myclient.Port = 25;
//验证登录
myclient.Credentials = new System.Net.NetworkCredential("xxxx@", "*******");//这里是我自己的outlook账号和密码。
myclient.Send(mymail); //vs下会在这行报上面那个错。
}
}
------解决思路----------------------
outlook没试过,要不要设置开启stmp服务,这是我的可用的,用的网易126
string from = txtFrom.Text;
string fromname = txtDisplayName.Text;
string subject = txtEmailTitle.Text;
string body = txtBody.Text;
string username = txtFrom.Text;
string password = txtPasswordFrom.Text;
string server = txtSmtp.Text;
int port = int.Parse(txtPort.Text);
//邮件发送类
MailMessage mail = new MailMessage();
//是谁发送的邮件
mail.From = new MailAddress(from, fromname);
//发送给谁
for (int i = 0; i < to.Length; ++i)
{
mail.To.Add(to[i]);
}
//标题
mail.Subject = subject;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = true;
//附件
//selectPath = AppDomain.CurrentDomain.BaseDirectory + selectPath;
if (!string.IsNullOrWhiteSpace(FilePath))
{
mail.Attachments.Add(new Attachment(FilePath));
}
//邮件服务器和端口
SmtpClient smtp = new SmtpClient(server,port);
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(username, password);
//超时时间
smtp.Timeout = 30000;
smtp.Send(mail);
return "ok";