[英]C# SMTP mail sending
I wish to send an email by C# console application. 我希望通过C#控制台应用程序发送电子邮件。 I found source codes - which are mostly look alike.But my problem I think, I dont know the how- to of this issue.
我找到了源代码-大部分看起来都差不多。但是我想我的问题是,我不知道该问题的用法。
So, how can I overcome this problem ? 那么,我该如何克服这个问题呢?
Thanks .. 谢谢 ..
That´s exactly what I do to send emails. 这正是我发送电子邮件的方式。 Here the code (you have to replace port , Server and enablessl with the correct values)
这里的代码(您必须使用正确的值替换port , Server和enablesl )
public static void SendMail(string addressTo, string addressFrom, string mailSubject, string mailBody)
{
NetworkCredential myCredential = new NetworkCredential(_userName, _passWord);
SmtpClient client = new SmtpClient();
client.Host = "99.99.127.233";
client.Port = 417;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = myCredential;
client.EnableSsl = true;
MailAddress from = new MailAddress(addressFrom);
MailAddress to = new MailAddress(addressTo);
MailMessage message = new MailMessage(from, to);
message.Body = mailBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = mailSubject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.Send(message);
}
I was having the same problem with smtp.gmail.com. 我在smtp.gmail.com上遇到了同样的问题。 I enabled "Access for less secure apps" and turned off 2-step verification.
我启用了“访问不太安全的应用程序”并关闭了两步验证。 It worked.
有效。
try this code 试试这个代码
MailMessage message = new MailMessage("sender@gmail.com", "receiver@gmail.com");
message.Subject = "Subject";
string body = "Mail Body";
message.Body = body;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
NetworkCredential networkCredentials = new NetworkCredential("sender@gmail.com", "<password>");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredentials;
smtpClient.Port = 587;
smtpClient.Send(message);
I'm doing a similar thing at the moment, just in a Windows Form. 我现在正在做类似的事情,只是在Windows窗体中。 I use the library EASendMail ( https://www.emailarchitect.net/easendmail ).
我使用图书馆EASendMail( https://www.emailarchitect.net/easendmail )。 It has various classes such as a standard Mail class that you can use like this:
它具有各种类,例如标准Mail类,您可以像这样使用:
void SmtpSend(string from, string to, string subject, string body)
{
// \/ Use
EASendMail.SmtpMail testMail = new EASendMail.SmtpMail("test");
testMail.From = from;
testMail.To = to;
testMail.Subject = subject;
testMail.TextBody = body;
// Alternatively, you can use HTML bodies with testMail.HtmlBody = "<html><head></head><body></body>" etc.
// Just play around with it.
// \/ Use the SmtpClient class to send your SmtpMail objects \/
EASendMail.SmtpClient smtpClient = new EASendMail.SmtpClient();
smtpClient.Credentials = new NetworkCredentials("username", "password");
smtpClient.SendMail(testMail);
}
void main()
{
SmtpSend("sender@yourapp.com", "reciever@yourapp.com", "Test Mail", "Test Body");
}
You can also use the SmtpServer
object if your want to connect to a server directly: 如果要直接连接到服务器,也可以使用
SmtpServer
对象:
SmtpServer smtpgm = new SmtpServer("smtp.gmail.com");
EASendMail.SmtpClient cln = new EASendMail.SmtpClient();
smtpgm.Port = 587;
smtpgm.User = Console.ReadLine();
smtpgm.Password = Console.ReadLine();
smtpgm.Alias = "W";
smtpgm.Protocol = ServerProtocol.SMTP;
smtpgm.ConnectType = SmtpConnectType.ConnectSTARTTLS;
If this message is being sent to postfix , hosted by yourself (or someone you can get assistance from), make sure to enable the login
SASL authentication scheme. 如果此消息发送给您自己(或您可以从中获得帮助的人)托管的postfix ,请确保启用
login
SASL身份验证方案。 By default, usually only plain
is enabled. 默认情况下,通常仅启用
plain
文本。
Otherwise your requests will be treated as anonymous (regardless of your credential settings on SmtpClient
), and the remaining of the configuration will likely cause your request to be blocked. 否则,您的请求将被视为匿名 (无论您在
SmtpClient
上的凭据设置如何),并且其余的配置可能会导致您的请求被阻止。
TL;DR TL; DR
Troubleshooting can be done on the postfix machine, by enabling verbose debugging in your postfix configuration. 通过在postfix配置中启用详细调试,可以在postfix机器上进行故障排除。 This can be done usually /etc/postfix/main.cf, by adding this line: (with
81.82.83.84
being the (public) IP-address of the machine running your SmtpClient code) 通常可以通过添加以下行来在/etc/postfix/main.cf中完成此操作:(其中
81.82.83.84
是运行SmtpClient代码的计算机的(公共)IP地址)
debug_peer_list = 81.82.83.84
Than let your SmtpClient
make a new request, while investigating your logging (usually in /var/log/mail.info
). 在调查您的日志记录时(通常在
/var/log/mail.info
),不要让SmtpClient
发出新请求。
A successful session will show an incoming ( <
) AUTH login
request, resulting in a successful reply ( Authentication successful
) 成功的会话将显示传入的(
<
) AUTH login
请求,从而导致成功的答复( Authentication successful
)
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-smtpserver.smtpdomain.com
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-PIPELINING
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-SIZE 20480000
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-VRFY
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-ETRN
# Postfix tells SmtpClient that it can use AUTH "LOGIN" scheme
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-AUTH PLAIN LOGIN
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-AUTH=PLAIN LOGIN
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-ENHANCEDSTATUSCODES
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250-8BITMIME
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 250 DSN
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: watchdog_pat: 0x55c423dbbdd0
# AUTH request
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: < unknown[81.82.83.84]: AUTH login b3JnYW5pbW1vQGZveGlubm9xxx
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: query milter states for other event
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: milter8_other_event: milter inet:localhost:8891
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: xsasl_dovecot_server_first: sasl_method login, init_response b3JnYW5pbW1vQGZveGlubm9xxx
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: xsasl_dovecot_handle_reply: auth reply: CONT?1?UGFzc3dv
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 334 UGFzc3dv
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: < unknown[81.82.83.84]: QXplcnR5MDE=
# AUTH reply
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: xsasl_dovecot_handle_reply: auth reply: OK?1?user=authenticationuser
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: > unknown[81.82.83.84]: 235 2.7.0 Authentication successful
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: watchdog_pat: 0x55c423dbbdd0
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: < unknown[81.82.83.84]: MAIL FROM:<user@destinationdomain.com>
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: extract_addr: input: <user@destinationdomain.com>
Jul 10 08:04:20 smtpserver postfix/submission/smtpd[13312]: smtpd_check_addr: addr=user@destinationdomain.com
Without enabling the login
authentication scheme, the 250-AUTH
response will only contain PLAIN
, and the SmtpClient
will not authenticate. 如果不启用
login
身份验证方案,则250-AUTH
响应将仅包含PLAIN
,并且SmtpClient
将不进行身份验证。
As said, this will likely cause your message to be blocked by helo, client, relay, sender or recipient restrictions. 如前所述,这很可能导致您的邮件被Helo,客户端,中继,发件人或收件人限制所阻止。
If your postfix uses dovecot for SASL authentication, enabling the login
SASL authentication scheme can be done in /etc/dovecot/conf.d/10-auth.conf
: 如果您的后缀使用dovecot进行SASL身份验证,则可以在
/etc/dovecot/conf.d/10-auth.conf
启用login
SASL身份验证方案:
auth_mechanisms = plain login cram-md5
(And make sure only one such configuration exists in your configuration files.) (并确保配置文件中仅存在一种这样的配置。)
More information can be found here: https://wiki2.dovecot.org/Authentication/Mechanisms (where it is shown to also enable cram-md5
beside plain
and login
) 可以在以下位置找到更多信息: https : //wiki2.dovecot.org/Authentication/Mechanisms (显示为在
plain
和login
旁边还启用cram-md5
)
If your message still gets blocked further on, you can add permit_sasl_authenticated
as the first restriction of the postfix restriction settings - at least temporarily - for further investigation. 如果您的消息仍被进一步阻止,则可以添加
permit_sasl_authenticated
作为后缀限制设置的第一个限制(至少是暂时的),以进行进一步调查。 eg (in my case I chose to put it after permit_mynetwork
to allow sending mails from local host) 例如(在我的情况下,我选择将其放在
permit_mynetwork
之后,以允许从本地主机发送邮件)
smtpd_helo_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_non_fqdn_helo_hostname,reject_invalid_helo_hostname,permit
smtpd_sender_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_non_fqdn_sender,reject_unknown_sender_domain,permit
#...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.