6

I am trying to send mail through Gmail. I am sending mail successfully when I am testing on localhost, but this does not work when I upload it to a web host. I am seeing this type of error:

Request for the permission of type System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.

Whenever I am using port 25 get this type of error below:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

Below is my code of send email.

MailMessage mail = new MailMessage("host@gmail.com","User@gamil.com");

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");              
mail.Subject = "Any String" 
mail.Body = mailbody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com","123");               
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);

Is there any solution? Please suggest to me!

Vishal Parmar
  • 524
  • 7
  • 27
  • Is it throwing an exception? If so what type? – MindSwipe Oct 16 '18 at 06:32
  • Request for the permission of type System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. – Vishal Parmar Oct 16 '18 at 06:34
  • Problem is with network credentials, make sure that your password is more complex and add stuff like `@` ... it's gmail security, it won't let you send mail until your password is complex enough – Veljko89 Oct 16 '18 at 06:34
  • 1
    Try changing `SmtpServer.Port = 587` to `SmtpServer.Port = 25`. 25 is the default port for SMTP and changing it requires elevated permission – MindSwipe Oct 16 '18 at 06:35
  • @Veljko89 I think (and certainly hope) OP isn't actually using that password and just put it there as a placeholder – MindSwipe Oct 16 '18 at 06:36
  • I hope so too @MindSwipe – Veljko89 Oct 16 '18 at 06:37
  • @MindSwipe You are Right.i am actually not use this password. – Vishal Parmar Oct 16 '18 at 06:39
  • The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – Vishal Parmar Oct 16 '18 at 06:56
  • Possible Duplicate of https://stackoverflow.com/questions/29958229/send-email-using-gmail-error-the-server-response-was-5-5-1-authentication-req – MindSwipe Oct 16 '18 at 07:08
  • @VishalParmar change password, use something more complex, use some online password generator (with special characters, to be at least 10 length) and it will work ... give it a shot – Veljko89 Oct 16 '18 at 07:14
  • @Velijko89 I have 14 length password.combination of special characters,alphabet or number – Vishal Parmar Oct 16 '18 at 07:17

3 Answers3

4

Edit: OP Added extra information crucial to answering this question, but I'm keeping the old answer around as it might still help someone

New Answer: This StackOverflow question already answered this question

OldAnswer: As this StackOverflow answer already answered, you changed the Port on the SMTP Server to 587 instead of its default (25) and this requires elevated permissions causing this error change this:

SmtpServer.Port = 587;

to this:

SmtpServer.Port = 25;

and it should work

Note: When using SSL the port needs to be 443

MindSwipe
  • 7,193
  • 24
  • 47
0

Answer : Your code add SmtpDeliveryFormat.SevenBit

Example:

using (SmtpClient smtp = new SmtpClient())
                {
                    NetworkCredential credential = new NetworkCredential
                    {
                        UserName = WebConfigurationManager.AppSettings["UserName"],
                        Password = WebConfigurationManager.AppSettings["Password"],
                    };
                    smtp.Credentials = credential;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
                    smtp.Host = WebConfigurationManager.AppSettings["Host"];
                    smtp.Port = WebConfigurationManager.AppSettings["Port"].ToNcInt();
                    smtp.EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]);
                    smtp.Send(mail);
                }
-1

Try This

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

namespace AmazonSESSample
{
    class Program
{
    static void Main(string[] args)
    {
        // Replace sender@example.com with your "From" address. 
        // This address must be verified with Amazon SES.
        String FROM = "a@a.com";
        String FROMNAME = "ABC";

        // Replace recipient@example.com with a "To" address. If your account 
        // is still in the sandbox, this address must be verified.
        String TO = "a@a.com";

        // Replace smtp_username with your Amazon SES SMTP user name.
        String SMTP_USERNAME = "a@a.com";

        // Replace smtp_password with your Amazon SES SMTP user name.
        String SMTP_PASSWORD = "ASJKAJSN";

        // (Optional) the name of a configuration set to use for this message.
        // If you comment out this line, you also need to remove or comment out
        // the "X-SES-CONFIGURATION-SET" header below.
        String CONFIGSET = "ConfigSet";

        // If you're using Amazon SES in a region other than US West (Oregon), 
        // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP  
        // endpoint in the appropriate AWS Region.
        String HOST = "smtp-relay.sendinblue.com";

        // The port you will connect to on the Amazon SES SMTP endpoint. We
        // are choosing port 587 because we will use STARTTLS to encrypt
        // the connection.
        int PORT = 587;

        // The subject line of the email
        String SUBJECT =
            "Amazon SES test (SMTP interface accessed using C#)";

        // The body of the email
        String BODY =
            "<h1>Amazon SES Test</h1>" +
            "<p>This email was sent through the " +
            "<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
            "using the .NET System.Net.Mail library.</p>";

        // Create and build a new MailMessage object
        MailMessage message = new MailMessage();
        message.IsBodyHtml = true;
        message.From = new MailAddress(FROM, FROMNAME);
        message.To.Add(new MailAddress(TO));
        message.Subject = SUBJECT;
        message.Body = BODY;
        // Comment or delete the next line if you are not using a configuration set
        message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);

        using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
        {
            // Pass SMTP credentials
            client.Credentials =
                new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);

            // Enable SSL encryption
            client.EnableSsl = true;

            // Try to send the message. Show status in console.
            try
            {
                Console.WriteLine("Attempting to send email...");
                client.Send(message);
                Console.WriteLine("Email sent!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("The email was not sent.");
                Console.WriteLine("Error message: " + ex.Message);
            }
        }
    }
}

}

sanvssan
  • 34
  • 3