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

有可能做到吗?


当前回答

如果您想发送后台邮件,请执行以下操作

 public void SendEmail(string address, string subject, string message)
 {
 Thread threadSendMails;
 threadSendMails = new Thread(delegate()
    {

      //Place your Code here 

     });
  threadSendMails.IsBackground = true;
  threadSendMails.Start();
}

并添加命名空间

using System.Threading;

其他回答

包括这个,

using System.Net.Mail;

然后,

MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body); 
SmtpClient client = new SmtpClient("smtp.gmail.com");

client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","password");
client.EnableSsl = true;

client.Send(sendmsg);

在Gmail / Outlook.com邮箱更改发件人:

为了防止欺骗- Gmail/Outlook.com不会让你从任意用户帐户名发送。

如果发件人数量有限,则可以按照以下说明将From字段设置为此地址:从不同地址发送邮件

如果你想从一个任意的电子邮件地址发送(比如用户在网站上输入他们的电子邮件,而你不希望他们直接给你发送电子邮件),你能做的最好的是:

        msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));

这可以让你在电子邮件账户中点击“回复”,在反馈页面上回复你乐队的粉丝,但他们不会收到你的实际电子邮件,这可能会导致大量垃圾邮件。

如果你在一个受控制的环境中,这工作得很好,但请注意,我看到一些电子邮件客户端发送到from地址,即使回复指定(我不知道是哪个)。

这是不再支持,如果你正在尝试这样做现在。

https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637960864118404117-800836189&p=less-secure-apps&rd=1#zippy=

如果你的谷歌密码不起作用,你可能需要在谷歌上为Gmail创建一个特定于应用程序的密码。 https://support.google.com/accounts/answer/185833?hl=en

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

namespace SendMailViaGmail
{
   class Program
   {
   static void Main(string[] args)
   {

      //Specify senders gmail address
      string SendersAddress = "Sendersaddress@gmail.com";
      //Specify The Address You want to sent Email To(can be any valid email address)
      string ReceiversAddress = "ReceiversAddress@yahoo.com";
      //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
      const string SendersPassword = "Password";
      //Write the subject of ur mail
      const string subject = "Testing";
      //Write the contents of your mail
      const string body = "Hi This Is my Mail From Gmail";

      try
      {
        //we will use Smtp client which allows us to send email using SMTP Protocol
        //i have specified the properties of SmtpClient smtp within{}
        //gmails smtp server name is smtp.gmail.com and port number is 587
        SmtpClient smtp = new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials = new NetworkCredential(SendersAddress, SendersPassword),
           Timeout = 3000
        };

        //MailMessage represents a mail message
        //it is 4 parameters(From,TO,subject,body)

        MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
        /*WE use smtp sever we specified above to send the message(MailMessage message)*/

        smtp.Send(message);
        Console.WriteLine("Message Sent Successfully");
        Console.ReadKey();
     }
     catch (Exception ex)
     {
        Console.WriteLine(ex.Message);
        Console.ReadKey();
     }
}
}
}