C#发送电子邮件 - 无法在C#.Net中发送电子邮件 - 尽快帮助! [英] C# Send Email - Can't Send Email in C# .Net - Help ASAP!

查看:82
本文介绍了C#发送电子邮件 - 无法在C#.Net中发送电子邮件 - 尽快帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码返回False?

两个电子邮件帐户密码都是一样的我自己都有!



 内部 静态 布尔 __send_email( String  yourEmailHost, String  usersEmailAddress, String  userMessageSubject, String  userMessageBody, String  yourEmailAddress, String  yourEmailAddressPassword, UInt16  yourEmailProvidersSMTPPortNumber)
{
string smtpAddress = yourEmailHost;
int portNumber = yourEmailProvidersSMTPPortNumber;
bool enableSSL = true ;

string emailFrom = shelbypurcell000@gmail.com;
string password = ; // 密码在这里
string emailTo = shelby.p67@gmail.com;
string subject = userMessageSubject;
string body = userMessageBody;

使用(MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true ;
// 如果您要发送纯文本,可以设置为false。
使用(SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom,password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
return true ;
}
}
}

解决方案

试试这个



  private   void  button1_Click( object  sender,EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient( smtp .gmail.com);

mail.From = new MailAddress( someone@gmail.com);
mail.To.Add( someone@mail.com);
mail.Subject = 测试邮件;
mail.Body = 这是用于测试来自C#Project的SMTP邮件;

SmtpServer.Port = 587 ;
SmtpServer.Credentials = new System.Net.NetworkCredential( someone@gmail.com password);
SmtpServer.EnableSsl = true ;

SmtpServer.Send(mail);
MessageBox.Show( mail send);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}


Google Gmail登录机制需要更多步骤才能运作。



要么你实施他们的安全登录程序要么禁用安全登录。



禁用安全登录是更简单的方法,但安全性较低。



关于关闭安全登录:



Google可能阻止某些人的登录尝试不使用现代安全标准的应用程序或设备。由于这些应用和设备更容易入侵,因此阻止它们有助于保持帐户安全。



不支持最新安全标准的应用程序的一些示例包括:



iOS或更低版本的iPhone或iPad上的邮件应用程序

8.1版本之前的Windows手机上的邮件应用程序

某些桌面邮件客户端,如Microsoft Outlook和Mozilla Thunderbird

因此,您必须在Google帐户中启用Less Secure Sign-In。



登录Google帐户后,请访问:



https://www.google.com/settings/security/lesssecureapps


< pre lang =   C# > MailMessage Msg =  new  MailMessage(); 
// 发件人电子邮件地址。
Msg.From = new MailAddress(邮件ID在这里);
// 收件人电子邮件地址。
Msg.To.Add(邮件ID);
Msg.Subject =& quot;添加主题& quot ;;
Msg.Body =& quot; Hi,& lt; br /& gt;& lt; br /& gt; Detailss& lt; br /& gt;& lt; br / & gt;:& quot;& quot;& quot ;;
Msg.IsBodyHtml = true ;
// 您的远程SMTP服务器IP。
SmtpClient smtp = new SmtpClient();
smtp.Host =& quot; smtp.gmail.com& quot ;;
smtp.Port = 587 ;
smtp.Credentials = new System.Net.NetworkCredential(& quot; from 邮件ID此处&安培; QUOT;,&安培; QUOT; passwordhere&安培; QUOT);
smtp.EnableSsl = true ;
smtp.Send(Msg);
ScriptManager.RegisterStartupScript(Page,GetType(),& quot; MyScript& quot;,& quot; alert(&#39;您的密码详情发送到您的邮件&#39;);& quot; ;, true );
& quot; e& quot;);
; < / pre > < /跨度>


Why does this code return False?
Both Email Account Passwords Are The Same And I Own Them Both!

internal static Boolean __send_email(String yourEmailHost,String usersEmailAddress, String userMessageSubject, String userMessageBody, String yourEmailAddress, String yourEmailAddressPassword, UInt16 yourEmailProvidersSMTPPortNumber)
        {
            string smtpAddress = yourEmailHost;
            int portNumber = yourEmailProvidersSMTPPortNumber;
            bool enableSSL = true;

            string emailFrom = "shelbypurcell000@gmail.com";
            string password = "";//Password Here
            string emailTo = "shelby.p67@gmail.com";
            string subject = userMessageSubject;
            string body = userMessageBody;

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                // Can set to false, if you are sending pure text.
                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                    return true;
                }
            }
        }

解决方案

Try This

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        mail.From = new MailAddress("someone@gmail.com");
        mail.To.Add("someone@mail.com");
        mail.Subject = "Test Mail";
        mail.Body = "This is for testing SMTP mail from C# Project";

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("someone@gmail.com", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}


Google Gmail login mechanism require more steps for it to works.

It's either you implement their secure login procedure or disable secure login.

Disable secure login is the simpler way but less security.

About turn off secure sign in:

Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.

Some examples of apps that do not support the latest security standards include:

The Mail app on your iPhone or iPad with iOS 6 or below
The Mail app on your Windows phone preceding the 8.1 release
Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
Therefore, you have to enable Less Secure Sign-In in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps


<pre lang="C#">MailMessage Msg = new MailMessage();
               // Sender e-mail address.
               Msg.From = new MailAddress(mail id here);
               // Recipient e-mail address.
               Msg.To.Add(mail id);
               Msg.Subject = &quot;Add Subject&quot;;
               Msg.Body = &quot;Hi, &lt;br/&gt;&lt;br/&gt;Detailss&lt;br/&gt;&lt;br/&gt;:  &quot;&quot;&quot;;
               Msg.IsBodyHtml = true;
               // your remote SMTP server IP.
               SmtpClient smtp = new SmtpClient();
               smtp.Host = &quot;smtp.gmail.com&quot;;
               smtp.Port = 587;
               smtp.Credentials = new System.Net.NetworkCredential(&quot;from Mail id here&quot;,&quot;passwordhere&quot;);
               smtp.EnableSsl = true;
               smtp.Send(Msg);
               ScriptManager.RegisterStartupScript(Page, GetType(), &quot;MyScript&quot;, &quot;alert(&#39;Your Password Details Sent to your mail&#39;);&quot;, true);
               &quot;e&quot;);
              ;</pre>


这篇关于C#发送电子邮件 - 无法在C#.Net中发送电子邮件 - 尽快帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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