[英]sending mail in asp.net-also using web config
i am tryiung to create a contact us page ,where the user clicks submit and sends an email to me, i looked at some examples, but they seem to be hard coding their email credentials into the code, i found out that for security m you can store the username and password in the webconfig file, below is my web config code and my default aspx.cs code, could anybody please help me solve the problem, this is the error i get 我正在尝试创建一个联系我们页面,用户点击提交并向我发送电子邮件,我看了一些示例,但他们似乎很难将他们的电子邮件凭据编码到代码中,我发现为了安全,你可以在webconfig文件中存储用户名和密码,下面是我的web配置代码和我的默认aspx.cs代码,任何人都可以帮我解决问题,这是我得到的错误
The remote name could not be resolved: 'smtp.gmail.com,587' Line 45: mailClient.Send(message);
无法解析远程名称:'smtp.gmail.com,587'第45行:mailClient.Send(message);
Here is my appsettings and code: 这是我的appsettings和代码:
<appSettings>
<add key="PFUserName" value="myemail@gmail.com"/>
<add key="PFPassWord" value="mypassword"/>
<add key="MailServerName" value="smtp.gmail.com,587"/>
</appSettings>
using System;
using System.Data;
using System.Configuration;
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.Mail;
using System.Web.Configuration;
using System.Net;
namespace WebApplication2
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
SendMail(txtEmail.Text, txtComments.Text);
}
private void SendMail(string from, string body)
{
string Username = WebConfigurationManager.AppSettings["PFUserName"].ToString();
string Password = WebConfigurationManager.AppSettings["PFPassWord"].ToString();
string MailServer = WebConfigurationManager.AppSettings["MailServerName"].ToString();
NetworkCredential cred = new NetworkCredential(Username, Password);
string mailServerName = ("smtp.gmail.com,587");
MailMessage message = new MailMessage(from, Username, "feedback", body);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com,587");
mailClient.EnableSsl = true;
mailClient.Host = mailServerName;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = cred;
mailClient.Send(message);
message.Dispose();
}
}
} }
You need to set SMTP setting inside the mailSettings
configuration in web.config like this 您需要在web.config中的
mailSettings
配置中设置SMTP设置,如下所示
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="my@mail.com">
<network host="smtp.gmail.com" port="587" userName="myemail@gmail.com" password="mypassword" />
</smtp>
</mailSettings>
</system.net>
Your server name is smtp.gmail.com
(remove the 587 from there). 您的服务器名称是
smtp.gmail.com
(从那里删除587)。 587 is the port that smtp is using. 587是smtp正在使用的端口。 So put this value in host property.
所以把这个值放在host属性中。
C# Code: C#代码:
SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("sender@mail.com"));
mailMessage.Subject = "mailSubject";
mailMessage.Body = "mailBody";
smtpClient.Send(mailMessage);
This is what I currently use in my Web Config with some obvious edits 这是我目前在Web Config中使用的一些明显的编辑
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="username@gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587" userName=username" password="xxxxxxxxxxxx" />
</smtp>
</mailSettings>
</system.net>
in the CS file 在CS文件中
using System.Net.Mail;
and 和
MailMessage myMessage = new MailMessage();
//subject line
myMessage.Subject = Your Subject;
//whats going to be in the body
myMessage.Body = Your Body Info;
//who the message is from
myMessage.From = (new MailAddress("Mail@Mail.com"));
//who the message is to
myMessage.To.Add(new MailAddress("Mail@Mail.com"));
//sends the message
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
for sending. 发送。
您的主机名应为“smtp.gmail.com”,然后将mailClient.Port
设置为587。
why did you dont compile in a class to make a dll? 为什么你不在类中编译来制作一个DLL?
Well, i use this code, enjoy :) 好吧,我用这个代码,享受:)
MailMessage mail = new MailMessage();
try
{
mail.To.Add(destinatario); // where will send
mail.From = new MailAddress("email that will send", "how the email will be displayed");
mail.Subject = "";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
// mail body
mail.Body = "email body";
// send email, dont change //
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("gmail account", "gmail pass"); // set 1 email of gmail and password
client.Port = 587; // gmail use this port
client.Host = "smtp.gmail.com"; //define the server that will send email
client.EnableSsl = true; //gmail work with Ssl
client.Send(mail);
mail.Dispose();
return true;
}
catch
{
mail.Dispose();
return false;
}
<configuration>
<!-- Add the email settings to the <system.net> element -->
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
<system.web>
...
</system.web>
</configuration>
above is web.config
and here is back-end code : 上面是
web.config
,这里是后端代码:
using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.Configuration;
using System.Data.SqlClient;
using System.Net.Configuration;
private void Email(string email, string pass, string customername)
{
SmtpSection smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
MailMessage mm = new MailMessage(smtp.Network.UserName,email);
mm.Subject = "Thank you for Registering with us";
mm.Body = string.Format("Dear {0},<br /><br />Thank you for Registering with <b> Us. </b><br /> Your UserID is <b>{1}</b> and Password is <b> {2} </b> <br /><br /><br /><br /><br /><b>Thanks, <br />The Ismara Team </b>", customername, email, pass);
mm.IsBodyHtml = true;
try
{
using (SmtpClient client = new SmtpClient())
{
client.Send(mm);
}
}
catch (Exception ex)
{
throw new Exception("Something went wrong while sending email !");
}
}
system will automatically get the details of smtp from web.config
系统将自动从
web.config
获取smtp的详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.