[英]send email from asp.net app
我使用c#配置發送電子郵件的所有設置但是當我執行時我得到以下錯誤請求的地址在其上下文中無效74.125.53.109:25
我的代碼是
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);
Web.Config中
<appSettings>
<add key="SMTP" value="smtp.gmail.com"/>
<add key="FROMEMAIL" value="mail@gmail.com"/>
<add key="FROMPWD" value="password"/>
</appSettings>
通過Gmail在.NET中發送電子郵件此鏈接有一個完整的代碼,用於通過我的電腦中的gmail發送電子郵件及其正常工作的電子郵件 。
端口25是默認端口,但不是通過SSL發送電子郵件的正確端口。 由於您使用的是SSL,因此需要設置smtp.Port = 465
,根據Google主題的幫助頁面: http : //support.google.com/mail/bin/answer.py? smtp.Port = 465
我認為該地址無效,因為它在SSL連接的上下文中以端口25為目標。
我想我會在這里發布帖子的綜合努力:
將其添加到配置文件中,更改電子郵件/用戶名/密碼。 您可能必須根據Brian Rogers發布的內容更改端口。
<system.net>
<mailSettings>
<smtp from="some.user@gmail.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" enableSsl="true" userName="some.user@gmail.com" password="mypassword"/>
</smtp>
</mailSettings>
</system.net>
在代碼中使用它
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
這是一個適用於發送郵件的功能,我檢查了它並且它正在工作。
private static bool testsendemail(MailMessage message)
{
try
{
MailMessage message1 = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
MailAddress fromAddress = new MailAddress("FromMail@Test.com");
message1.From = fromAddress;
message1.To.Add("ToMail@Test1.com");
message1.Subject = "This is Test mail";
message1.IsBodyHtml = true;
message1.Body ="You can write your body here" + message;
// We use yahoo as our smtp client
smtpClient.Host = "smtp.mail.yahoo.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(
"SenderMail@yahoo.com",
"YourPassword"
);
smtpClient.Send(message1);
}
catch
{
return false;
}
return true;
}
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 btnSend_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("xxx@yourdomain.com");
msg.To.Add(txtTo.Text); //Text Box for To Address
msg.Subject = txtSubject.Text; //Text Box for subject
msg.IsBodyHtml = true;
msg.Body = txtBody.Text; //Text Box for body
msg.Priority = MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(
"relay-hosting.secureserver.net",
25
);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(
"xxx@yourdomain.com",
"yourpassword"
);
client.Host = "relay-hosting.secureserver.net";
client.EnableSsl = false;
object userstate = msg;
client.Send(msg);
}
}
您無法在IP之后將端口25指定為:25
。
它將默認為端口25,因此您不需要它。 如果要更改端口,請使用以下命令:
mail.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"portnumber"
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.