可能重复:结果 发送电子邮件asp C#
我已经使用该技术前几次,但不知它不工作我提供了以下的code发送的邮件几次:
I have sent mail several times using the technique several times before but somehow it doesnt work i am providing the code in the following:
MailMessage myMailMessage = new MailMessage(); myMailMessage.Subject = "Response From Client"; myMailMessage.Body = "hello word"; myMailMessage.From = new MailAddress("mymail@gmail", "jub"); myMailMessage.To.Add(new MailAddress("mymail@gmail", "Softden")); SmtpClient mySmtpClient = new SmtpClient(); mySmtpClient.Send(myMailMessage);和我的web.config是:
and my web.config is:
<mailSettings> <smtp deliveryMethod = "Network" from="Jubair <mymail@gmail>"> <network defaultCredentials="false" enableSsl="true" userName="mymail@gmail" password="Mypassword" host="smtp.gmail" port="587"></network> </smtp> </mailSettings>它说的SMTP服务器要求安全连接或客户端没有经过认证。请帮助
it says the smtp server requires a secure connection or the client was not authenticated.. Please help
推荐答案尝试添加这样的事每多米尼克的#2回答来看,他下面的例子
Try adding something like this Per Dominic's answer on Stackoverflow look at he following example
using System.Net; using System.Net.Mail; var fromAddress = new MailAddress("from@gmail", "From Name"); var toAddress = new MailAddress("to@example", "To Name"); const string fromPassword = "fromPassword"; const string subject = "Subject"; const string body = "Body"; var smtp = new SmtpClient { Host = "smtp.gmail", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, //UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); }// -----------------下面一个简单的方法---------------我只是测试此之下,它的工作原理
//----------------- A simple approach below --------------- I just tested this below and it works
VAR邮件= MAILMESSAGE新();
var mail = new MailMessage();
// Set the to and from addresses. // The from address must be your GMail account mail.From = new MailAddress("noreplyXYZ@gmail"); mail.To.Add(new MailAddress(to)); // Define the message mail.Subject = subject; mail.IsBodyHtml = isHtml; mail.Body = message; // Create a new Smpt Client using Google's servers var mailclient = new SmtpClient(); mailclient.Host = "smtp.gmail";//ForGmail mailclient.Port = 587; //ForGmail // This is the critical part, you must enable SSL mailclient.EnableSsl = true;//ForGmail //mailclient.EnableSsl = false; mailclient.UseDefaultCredentials = true; // Specify your authentication details mailclient.Credentials = new System.Net.NetworkCredential("fromAddress@gmail", "xxxx123");//ForGmail mailclient.Send(mail); mailclient.Dispose();//本的.config设置有关于如何可以设置此,我怀疑这是你遇到的问题两个选项
//The .config settings there are two options on how you could set this up I am suspecting that this is the issue you are having
<system> <mailSettings> <smtp from="from@gmail" deliveryMethod="Network"> <network userName="from@gmail" password="mypassword" host="smtp.gmail" port="587"/> </smtp> </mailSettings> </system>或选项2
<configuration> <appSettings> <add key="smtpClientHost" value="mail.localhost"/> //SMTP Client host name <add key="portNumber" value="587"/> <add key="fromAddress" value="yourEmailAddress@gmail"/> </appSettings> </configuration>更多推荐
发送邮件使用C#asp.net
发布评论