使用 .NET 发送电子邮件 - 没那么容易
题
大约一年来,我在尝试使用 Visual C# 以编程方式发送和接收电子邮件时遇到了问题。我在网上找到的数百个示例中没有一个有效。不,我不只是复制和粘贴。我研究代码,并根据需要修改/添加/删除。
有人可以帮我解决这个问题吗?我正在尝试完成去年开始制作的一个简单的程序,但事实证明我几乎不可能弄清楚。
老实说,我不知道该怎么办了。该文档没有为我提供任何有用的信息,因为它们都没有起作用。我已经将示例代码提供给其他人使用,它对他们有用 - 但对我不起作用!这是如何运作的?
我不知道发送邮件是否取决于我的计算机有哪些安全/防火墙设置。但为了以防万一,我甚至暂时完全关闭了所有安全和防火墙设置,只是为了看看它是否会发送电子邮件。
我不再有它的代码,因为我刚刚开始尝试再次做这件事,如果有人可以帮助我完成这项工作,我将非常感激。
所以,我想做的就是:
创建一个带有 2 个按钮和一个文本框的简单表单。(当然,完成了)button1
检查电子邮件(但仅在消息框中显示主题和发件人,不下载消息)button2
将 textBox1 的内容发送到“username@bluebottle.com
"
我的服务器设置是:
Username username@bluebottle.com
Password ***********
IMAP/POP Server (Incoming): mail.bluebottle.com
SMTP Server (Outgoing): mail.bluebottle.com
SMTP should be port 25, 26 or 587
POP3 should be port 110, using SSL 995
IMAP should be port 143, using SSL 993
感谢您抽出时间阅读。如果我没有清楚地解释任何内容,请说出来,我会尽力为您解释得更有意义。
解决方案
没关系。我只是理解了它自己。这是一个简单的123!或者是ABC?我忘了是怎么回事。任何知识,任何人柜面的兴趣或需要知道如何发送电子邮件在C#中,这是对我工作:
string Sender = "username@domain.com";
string Username = "username";
string Password = "********";
string Recipient = "username@domain.com";
string Subject = "Enter subject here.";
string Message = "Enter message here.";
string Host = "mail.server.com";
int Port = 26;
using(MailMessage Mail =
new MailMessage(
Sender,
Recipient))
using (SmtpClient SmtpMail =
new SmtpClient(
Host,
Port))
{
Mail.Subject = Subject;
Mail.Body = Message;
SmtpMail.EnableSsl = true;
SmtpMail.Credentials =
new System.Net.NetworkCredential(
Username,
Password);
SmtpMail.Send(Mail);
}
请注意,以下使用指令需要在文档的顶部被宣布:
using System.Net.Mail;
编辑:使用模式的Docos: HTTP:// msdn.microsoft.com/en-us/library/yh598w02(VS.71).aspx
其他提示
自今年以来,微软向所有 Hotmail 用户提供了 pop3 和 SMTP 支持。
- POP3 服务器:pop3.live.com(端口 995)
- SMTP 服务器:smtp.live.com(端口25){注意:如果端口25在您的网络或ISP中被阻止,则可以将SMTP端口设置为使用TLS或SSL加密的587,具体取决于使用的客户端}
不隶属于 StackOverflow