简体   繁体   English

发送 email 不带 gmail

[英]send email without gmail

It seems many solutions involve using a server: smtp.gmail.com and System.Net.Mail.SmtpClient .似乎许多解决方案都涉及使用服务器: smtp.gmail.com 和System.Net.Mail.SmtpClient

Other's suggest using System.Web.Mail (which many say has been deprecated): here for example.其他人建议使用System.Web.Mail (许多人说已被弃用): 例如here。

In any case, when I tried it, I wasn't able to get either of the above examples to work.无论如何,当我尝试它时,我无法让上述任何一个示例工作。 I don't think it's a firewall problem.我认为这不是防火墙问题。 I am able to use outlook express to send emails through gmail.我可以使用 outlook express 通过 gmail 发送电子邮件。 The error in C# that I am given is that the target machine is actively refusing the connection.我给出的C#中的错误是目标机器正在主动拒绝连接。

Should I attempt to use outlook from the code?我应该尝试从代码中使用 outlook 吗? Would the best way to do that be through AutoIt or through some dll/com?最好的方法是通过 AutoIt 还是通过一些 dll/com? Why do I need a server in the first place?为什么我首先需要一台服务器? In C# I can download things from the web, why can't I just send specifically formatted webpackages (emails) directly from C#, without having to use some sort of server?在 C# 我可以从 web 下载东西,为什么我不能直接从 C# 发送特定格式的 web 包(电子邮件),而不必使用某种服务器?

you need smtp relay or gateway.您需要 smtp 继电器或网关。 I use http://sendgrid.com/ to send emails from the cloud, they have a free option.我使用http://sendgrid.com/从云端发送电子邮件,他们有一个免费选项。

there is also sample code on how to send emails, I re-factored their code so I could call it from other places.还有关于如何发送电子邮件的示例代码,我重构了他们的代码,以便我可以从其他地方调用它。

public partial class Email
{
/// <summary>
/// Generic Email method used to send email through SendGrid
/// </summary>
/// <param name="ToAddressName">ToAddress, DisplayName</param>
/// <param name="FromAddress">Email address of sender</param>
/// <param name="FromName">Display Name of sender</param>
/// <param name="SendCopy">Send BCC to FromAddress</param>
public void SendEmail(Dictionary<string, string> toAddressName,
                      String fromAddress, String fromName,
                      String subject, String message,
                      bool sendCopy)
{
  try
  {
    MailMessage mailMsg = new MailMessage();

    // To
    foreach (KeyValuePair<string, string> kvp in toAddressName)
    {
      string toAddress = kvp.Key;
      string displayName = kvp.Value;
      mailMsg.To.Add(new MailAddress(toAddress, displayName));
    }

    // From
    mailMsg.From = new MailAddress(fromAddress, fromName);

    if (sendCopy)
    {
      mailMsg.Bcc.Add(new MailAddress(fromAddress, fromName));
    }

    // Subject and multipart/alternative Body
    mailMsg.Subject = subject;

    string html = message;
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

    // Init SmtpClient and send
    SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));

    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("sendgridusername", "sendgridpassword");
    smtpClient.Credentials = credentials;

    smtpClient.Send(mailMsg);
  }
  catch (Exception ex)
  {
    Error error = new Error();
    error.ReportError(ex);
  }
}

(ps if you have any comments on my code I would love to hear it, thanks) (ps如果您对我的代码有任何意见,我很想听听,谢谢)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM