Can I send emails without authenticating on the SMTP server?

别等时光非礼了梦想. 提交于 2019-11-30 18:01:00

问题


i am creating simple email sending application. In my application when ever i send email i have to put my email address or password as from but i don't want to use password only want to put email

so

Can i send Email without using password using c#/.net application ?

this is my code:

   try
    {
        // setup mail message
        MailMessage message = new MailMessage();
        message.From = new MailAddress(textBox1.Text);
        message.To.Add(new MailAddress(textBox2.Text));
        message.Subject = textBox3.Text;
        message.Body = richTextBox1.Text;

        // setup mail client
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
        mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");

        // send message
        mailClient.Send(message);

        MessageBox.Show("Sent");
    }
    catch(Exception)
    {
        MessageBox.Show("Error");
    }

回答1:


Can i send Email without using password using c#/.net application ?

Yes, if you have access to an email gateway that doesn't require authentication you can simply do:

SmtpClient mailClient = new SmtpClient("your.emailgateway.com");
mailClient.Send(message);

Maybe your company or ISP can provide one for you?




回答2:


In general, you can, sure. In your concrete example code you are using GMail which does not allow anonymous sending.

From their references:

smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465

An additional comment regarding your catch clause:

In my opinion you are heavily misusing the exception idea. A better aproach would be something like:

catch(Exception x)
{
    var s = x.Message;
    if ( x.InnerException!=null )
    {
        s += Environment.NewLine + x.InnerException.Message;
    }

    MessageBox.Show(s);
}


来源:https://stackoverflow.com/questions/12490689/can-i-send-emails-without-authenticating-on-the-smtp-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!