发送邮件使用C#asp.net [英] Sending mail Using C# asp.net

查看:100
本文介绍了发送邮件使用C#asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  发送电子邮件asp.net 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.com", "jub");
            myMailMessage.To.Add(new MailAddress("mymail@gmail.com", "Softden"));

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMailMessage);

和我的web.config是:

and my web.config is:

<mailSettings>
      <smtp deliveryMethod = "Network" from="Jubair &lt;mymail@gmail.com&gt;">
        <network defaultCredentials="false" enableSsl="true" userName="mymail@gmail.com" password="Mypassword" host="smtp.gmail.com" 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.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               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.com");
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.com";//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.com", "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.net>
    <mailSettings>
      <smtp from="from@gmail.com" deliveryMethod="Network">
        <network userName="from@gmail.com" password="mypassword" host="smtp.gmail.com" port="587"/>
      </smtp>             
    </mailSettings>
  </system.net>

或选项2

<configuration>
    <appSettings>
        <add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
        <add key="portNumber" value="587"/>
        <add key="fromAddress" value="yourEmailAddress@gmail.com"/>
    </appSettings>
</configuration>

这篇关于发送邮件使用C#asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆