我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。

有可能做到吗?


当前回答

试试这个

public static bool Send(string receiverEmail, string ReceiverName, string subject, string body)
{
        MailMessage mailMessage = new MailMessage();
        MailAddress mailAddress = new MailAddress("abc@gmail.com", "Sender Name"); // abc@gmail.com = input Sender Email Address 
        mailMessage.From = mailAddress;
        mailAddress = new MailAddress(receiverEmail, ReceiverName);
        mailMessage.To.Add(mailAddress);
        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = true;

        SmtpClient mailSender = new SmtpClient("smtp.gmail.com", 587)
        {
            EnableSsl = true,
            UseDefaultCredentials = false,
            DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential("abc@gmail.com", "pass")   // abc@gmail.com = input sender email address  
                                                                           //pass = sender email password
        };

        try
        {
            mailSender.Send(mailMessage);
            return true;
        }
        catch (SmtpFailedRecipientException ex)
        { 
          // Write the exception to a Log file.
        }
        catch (SmtpException ex)
        { 
           // Write the exception to a Log file.
        }
        finally
        {
            mailSender = null;
            mailMessage.Dispose();
        }
        return false;
}

其他回答

从2022年6月1日起,谷歌增加了一些安全功能

谷歌不再支持使用仅要求您使用您的用户名和密码登录您的谷歌帐户或直接使用谷歌帐户的用户名和密码发送邮件的第三方应用程序或设备。但是您仍然可以通过您的gmail帐户使用生成应用程序密码发送电子邮件。

以下是生成新密码的步骤。

访问https://myaccount.google.com/security 打开两步验证。 如有需要,请电话确认。 点击“应用程序密码”,就在“2步验证”勾的下方。请求邮件应用程序的新密码。

现在我们要用这个密码来发邮件,而不是你原来的账号密码。

下面是发送邮件的示例代码

public static void SendMailFromApp(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body) {            
            var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                EnableSsl = true
            };                
            smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!
            var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));
            smtpClient.Send(message);
    }

你可以像下面这样调用方法

SendMailFromApp("smtp.gmail.com", 25, "mygmailaccount@gmail.com",
          "tyugyyj1556jhghg",//This will be generated by google, copy it here.
          "mailme@gmail.com", "New Mail Subject", "Body of mail from My App");

一定要使用System.Net。邮件,而不是已弃用的System.Web.Mail。使用System.Web.Mail进行SSL是一堆拙劣的扩展。

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);
}

此外,转到谷歌帐户>安全页面,并查看登录谷歌> 2步验证设置。

如果启用了,那么您必须生成一个密码,允许. net绕过2步验证。点击“Signing in To谷歌> App passwords”,选择App = Mail, device = Windows Computer,最后生成密码。在fromPassword常量中使用生成的密码,而不是标准的Gmail密码。 如果它被禁用,那么你必须打开Less secure app access,这是不推荐的!所以最好启用两步验证。

上面的答案不管用。必须设置DeliveryMethod = SmtpDeliveryMethod。网络,否则将返回“客户端未经过身份验证”错误。此外,暂停总是一个好主意。

修改后的代码:

using System.Net.Mail;
using System.Net;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@yahoo.com", "To Name");
const string fromPassword = "password";
const string subject = "test";
const string body = "Hey now!!";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

来源:在ASP发送电子邮件。净c#

下面是一个使用c#发送邮件的示例工作代码,在下面的例子中,我使用谷歌的smtp服务器。

代码是相当不言自明的,替换电子邮件和密码与您的电子邮件和密码值。

public void SendEmail(string address, string subject, string message)
{
    string email = "yrshaikh.mail@gmail.com";
    string password = "put-your-GMAIL-password-here";

    var loginInfo = new NetworkCredential(email, password);
    var msg = new MailMessage();
    var smtpClient = new SmtpClient("smtp.gmail.com", 587);

    msg.From = new MailAddress(email);
    msg.To.Add(new MailAddress(address));
    msg.Subject = subject;
    msg.Body = message;
    msg.IsBodyHtml = true;

    smtpClient.EnableSsl = true;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = loginInfo;
    smtpClient.Send(msg);
}

在谷歌更新后,这是使用c#或。net发送电子邮件的有效方法。

using System;
using System.Net;
using System.Net.Mail;

namespace EmailApp
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            String SendMailFrom = "Sender Email";
            String SendMailTo = "Reciever Email";
            String SendMailSubject = "Email Subject";
            String SendMailBody = "Email Body";

            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587);
                SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage email = new MailMessage();
                // START
                email.From = new MailAddress(SendMailFrom);
                email.To.Add(SendMailTo);
                email.CC.Add(SendMailFrom);
                email.Subject = SendMailSubject;
                email.Body = SendMailBody;
                //END
                SmtpServer.Timeout = 5000;
                SmtpServer.EnableSsl = true;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new NetworkCredential(SendMailFrom, "Google App Password");
                SmtpServer.Send(email);

                Console.WriteLine("Email Successfully Sent");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }

        }
    }
}

要创建应用程序密码,您可以按照这篇文章: https://www.techaeblogs.live/2022/06/how-to-send-email-using-gmail.html