سؤال

Possible Duplicate:
Send email using System.Net.Mail through gmail. (C#)

I am using this code to send email using C# .Net but it generates error at run-time. The Code is:

 MailMessage MyMailMessage = new MailMessage("example@gmail.com", "example@gmail.com",

"write your subject Here ", "Hi,This is the test message ");

MyMailMessage.IsBodyHtml = false;

NetworkCredential mailAuthentication = new NetworkCredential("example@gmail.com","xxxxxxxx");

    SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

    mailClient.EnableSsl = true;

    mailClient.UseDefaultCredentials = false;

    mailClient.Credentials = mailAuthentication;

    mailClient.Send(MyMailMessage);

After adding my gmail username and password and running the code an exception occurs at

mailClient.Send(MyMailMessage); 

with message

Failure sending mail.

I've tried port 25 too but the problem is still there.

Is there anything else required other than Visual Studio and .Net Frameworks? I've all the versions of .Net Frameworks.

Please Help me!

هل كانت مفيدة؟

المحلول

Try the following,

  var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(<type your email address>, <type your password>)
                    };
                    using (var message = new MailMessage(<type your email address>, toAddress)
                    {
                        Subject = <subject>,
                        Body = <body>
                    })
                    {
                        smtp.Send(message);
                    }

نصائح أخرى

These are the settings that I use for sending email from GMail, place them in your web.config and then you only need to use new SmtpClient().Send(msg); for sending your mail. It's very convenient if you send mail from multiple places in your application. It also allows you to change your mail configuration without having to recompile your application

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network" >
                <network host="smtp.gmail.com" port="587" userName="an-email@gmail.com" password="********" enableSsl="true" defaultCredentials="false" />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

EDIT: Since you should already have the configuration element in your web.config, be sure to only copy/paste the system.net element, I usually put it directly below system.web

https://stackoverflow.com/a/489594/707000

"The above answer doesn't work. You have to set "DeliveryMethod = SmtpDeliveryMethod.Network" or it will come back with a "client was not authenticated" error. Also it's always a good idea to put a timeout."

a quick search shows most examples do not explicitly set mailClient.UseDefaultCredentials = false; try removing this line and see if it works.

in the examples I saw they used both port 587 and 25.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top