发送邮件使用C#asp.net

编程入门 行业动态 更新时间:2024-10-26 18:19:18
本文介绍了发送邮件使用C#asp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复:结果  发送电子邮件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 &lt;mymail@gmail&gt;"> <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

本文发布于:2023-10-29 13:18:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送邮件   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!