Home Can SmtpClient also send email directly to the receiver?
Reply: 0

Can SmtpClient also send email directly to the receiver?

user2464
1#
user2464 Published in June 20, 2018, 10:57 pm

SmptClient can be used to send an email via relay server. But is SmtpClient also able to send email directly to the receiver and not via relay server?

EDIT

Any ideas how SmtpCLient needs to be configured to be able to send emails directly to the receiver?

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."

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. 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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303015 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO