C# 我可以发送电子邮件而无需在 SMTP 服务器上进行身份验证吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12490689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:26:05  来源:igfitidea点击:

Can I send emails without authenticating on the SMTP server?

c#.netemailsmtpsmtpclient

提问by Jonas Gobel

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 ?

我可以使用 c#/.net 应用程序在不使用密码的情况下发送电子邮件吗?

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");
    }

采纳答案by Rickard

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

我可以使用 c#/.net 应用程序在不使用密码的情况下发送电子邮件吗?

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?

也许您的公司或 ISP 可以为您提供一个?

回答by Uwe Keim

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

一般来说,你可以,当然。在您的具体示例代码中,您使用的是不允许匿名发送的 GMail。

From their references:

他们的参考

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

smtp.gmail.com(使用身份验证)
使用身份验证:是
TLS/STARTTLS
端口:587 SSL 端口:465

An additional comment regarding your catchclause:

关于您的catch条款的附加评论:

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);
}