简体   繁体   English

SmtpClient也可以直接将电子邮件发送给收件人吗?

[英]Can SmtpClient also send email directly to the receiver?

SmptClient can be used to send an email via relay server . SmptClient可用于通过中继服务器发送电子邮件。 But is SmtpClient also able to send email directly to the receiver and not via relay server ? 但是SmtpClient还可以直接将电子邮件发送到收件人,而不是通过中继服务器

EDIT 编辑

Any ideas how SmtpCLient needs to be configured to be able to send emails directly to the receiver ? 有什么想法需要如何配置SmtpCLient才能将电子邮件直接发送到收件人

I tried with the following code but I got " The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. " 我尝试使用以下代码,但是得到了“ SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
     // var credentialUserName = "myAccount@gmail.com";
        var sentFrom = "myAccount@gmail.com";
     // var pwd = "myPwd";

        System.Net.Mail.SmtpClient client = 
            new System.Net.Mail.SmtpClient("smtp.gmail.com");

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

     /* System.Net.NetworkCredential credentials = 
            new System.Net.NetworkCredential(credentialUserName, pwd);
     */
        client.EnableSsl = true;
     // client.Credentials = credentials;

        var mail = 
            new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        return client.SendMailAsync(mail);
    }
}

SECOND EDIT: 第二编辑:

Thanx, it works now. 谢谢,现在可以使用。 App sent email directly ( and not via myAccount@gmail.com ) to otherAccount@gmail.com . 应用程序直接发送电子邮件(而不是通过myAccount@gmail.com )到otherAccount@gmail.com Here's the code: 这是代码:

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        var sentFrom = "myAccount@gmail.com";

        System.Net.Mail.SmtpClient client = 
            new System.Net.Mail.SmtpClient("gmail-smtp-in.l.google.com");

        client.Port = 25;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        client.EnableSsl = true;


        var mail = 
            new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        return client.SendMailAsync(mail);
    }
}

Thank you 谢谢

Short answer: yes! 简短的回答:是的!

A relay server is just a server that is configured to accept all your emails and pass them on to the right destination. 中继服务器只是配置为接受所有电子邮件并将它们传递到正确目的地的服务器。 You can equally well contact the right destination server directly and delivery the email there. 您同样可以直接与正确的目标服务器联系,然后在那儿发送电子邮件。

This, of course, provided there is no firewall issues preventing you from contacting the destination server directly. 当然,只要没有防火墙问题,您就无法直接与目标服务器联系。

EDIT 编辑

The server smtp.gmail.com is for gmail users to send (outgoing) emails, ie you must authenticate with your gmail username and password in order to be allowed to send an email that way, but if you do that you can send to any recipient, ie also non-gmail addresses. 服务器smtp.gmail.com是供gmail用户发送(发送)电子邮件的,即,您必须使用gmail用户名和密码进行身份验证才能被允许以这种方式发送电子邮件,但是如果这样做,则可以发送给任何收件人,即非Gmail地址。

I understood your original question to mean you would like to send emails to (in this case) a gmail-address without using a proxy. 我理解您的原始问题是指您希望不使用代理就将电子邮件发送到(在这种情况下)gmail地址。 In that case your client should behave as any arbitrary email server that is trying to send to a gmail-address, ie it should connect to one of official incoming SMTP servers for the domain as given by MX-records in the DNS. 在这种情况下,您的客户端应像试图发送到gmail地址的任意电子邮件服务器一样,即应连接到DNS中MX记录指定的域的官方传入SMTP服务器之一。 Eg one of gmail's MX-records points to gmail-smtp-in.l.google.com , and if you connect to port 25 of that server you can submit an email to a gmail-address (and you can also completely spoof the sending address, but then spam-filtering might cause your email to not be delivered). 例如,gmail的MX记录之一指向gmail-smtp-in.l.google.com ,如果您连接到该服务器的端口25,则可以向gmail地址提交电子邮件(而且您也可以完全欺骗发送地址,但随后垃圾邮件过滤可能会导致您的电子邮件无法发送)。

My caveat about firewall issues is to interpreted as this: most ISPs disallow outgoing TCP connections to port 25 to other hosts than their own servers. 我对防火墙问题的警告是这样解释的:大多数ISP都禁止将到端口25的传出TCP连接连接到其自身服务器以外的其他主机。 This is just because of the above mentioned spoofing possibility, ie if your ISP allows you to make TCP connections to port 25 of other email servers, you can use that to send spam. 这仅仅是由于上述欺骗可能性,即,如果您的ISP允许您建立与其他电子邮件服务器的端口25的TCP连接,则可以使用它来发送垃圾邮件。 Therefore your ISP might not allow you to do that, and instead you should relay your emails via your ISP so they can take appropriate measures if you try to spam people. 因此,您的ISP可能不允许您这样做,而应该通过ISP中继电子邮件,以便在您尝试向垃圾邮件发送者时采取适当的措施。

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

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