在C#中使用SMTP发送没有密码的电子邮件 [英] send an email using SMTP without password in C#

查看:554
本文介绍了在C#中使用SMTP发送没有密码的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ASP.net和C#的Web应用程序,第一步需要从用户

向带有附件的人发送电子邮件。
我的问题是用户何时发送电子邮件,我不想在用户每次发送时都输入
密码。
我想发送没有发件人密码的电子邮件。
是否可以使用SMTP做到这一点?
,这是我的代码并非全部的示例。
代码在我输入密码时可以正常工作,但是没有它,
不起作用,我需要一种使用smtp在不输入密码的情况下发送电子邮件但
的方法

I have a web application using ASP.net and C#,in one step it will need from the user to send an email to someone with an attachments. my problem is when the user will send the email i don't want to put their password every time the user send. i want to send an email without the password of the sender. any way to do that using SMTP ? and this is a sample of my code "not all". the code is worked correctly when i put my password , but without it ,it is not work, i need a way to send emails without put the password but in the same time using smtp protocol.

private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.office365.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "my email";
string password = "******";
string emailTo = "receiver mail";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
//  mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
{
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;               
smtp.Send(mail);
}
MessageBox.Show("message sent");
}
} 


推荐答案

I相信这可以轻松实现,但有一些限制。

I believe this can be accomplished easily, but with some restrictions.

请查看有关在配置文件中配置SMTP的MSDN文章

如果您的SMTP服务器允许,则您的电子邮件对象的发件人地址可能不需要与用于连接到SMTP服务器的凭据相同。

If your SMTP server allows it, your email object's from address may not need to be the same as the credentials used to connect to the SMTP server.

因此,将发件人设置为您已经是的电子邮件对象的地址:

So, set the from address of your email object as you already are:

mail.From = new MailAddress(emailFrom);

但是,配置smtp连接的方法有以下两种:

But, configure your smtp connection one of two ways:


  1. 将您的应用设置为在有权访问SMTP服务器的帐户下运行

  2. 在配置中包括SMTP服务器的凭据,< a href = https://msdn.microsoft.com/en-us/library/ms164242(v=vs.110).aspx rel = nofollow noreferrer>像这样。

  1. Set your app to run under an account that has permission to access the SMTP server
  2. Include credentials for the SMTP server in your config, like this.

然后,执行以下操作:

using (SmtpClient smtp = new SmtpClient())
{
    smtp.Send(mail);
}

让配置文件句柄为您设置SMTP。这也很棒,因为如果您切换服务器,则无需更改任何代码。

Let the configuration file handle setting up SMTP for you. This is also great because you don't need to change any of your code if you switch servers.

请记住,请注意配置文件中的任何敏感设置! (又名,不要将其签入公开的github存储库中)

Just remember to be careful with any sensitive settings in your config file! (AKA, don't check them into a public github repo)

这篇关于在C#中使用SMTP发送没有密码的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆