本文整理汇总了C#中SmtpClient.SendMailAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SmtpClient.SendMailAsync方法的具体用法?C# SmtpClient.SendMailAsync怎么用?C# SmtpClient.SendMailAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SmtpClient
的用法示例。
在下文中一共展示了SmtpClient.SendMailAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Send_Email
public async Task Send_Email(string toMail, string toSubject, string toMessage)
{
String Result = "";
try
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set your gmail email address
oMail.From = new MailAddress("[email protected]");
// Add recipient email address, please change it to yours
oMail.To.Add(new MailAddress(toMail));
// Set email subject and email body text
oMail.Subject = toSubject;
oMail.TextBody = toMessage;
// Gmail SMTP server
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// User and password for ESMTP authentication
oServer.User = "[email protected]";
oServer.Password = "123123fv";
// Enable TLS connection on 25 port, please add this line
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
await oSmtp.SendMailAsync(oServer, oMail);
Result = "Email was sent successfully!";
}
catch (Exception ep)
{
Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
}
// Display Result by Diaglog box
Windows.UI.Popups.MessageDialog dlg = new
Windows.UI.Popups.MessageDialog(Result);
await dlg.ShowAsync();
}
示例2: TestMailDeliveryAsync
public void TestMailDeliveryAsync()
{
SmtpServer server = new SmtpServer();
SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port);
MailMessage msg = new MailMessage("[email protected]", "[email protected]", "hello", "howdydoo");
Thread t = new Thread(server.Run);
t.Start();
Task task = client.SendMailAsync(msg);
t.Join();
server.Stop();
Assert.Equal("<[email protected]>", server.MailFrom);
Assert.Equal("<[email protected]>", server.MailTo);
Assert.Equal("hello", server.Subject);
Assert.Equal("howdydoo", server.Body);
Assert.True(task.Wait(1000));
}
示例3: btnSend_Tapped
//.........这里部分代码省略.........
{
MessageDialog dlg = new MessageDialog("Please input password!");
await dlg.ShowAsync();
textPassword.Password = "";
textPassword.Focus(Windows.UI.Xaml.FocusState.Programmatic);
return;
}
}
btnSend.IsEnabled = false;
pgBar.Value = 0;
try
{
SmtpClient oSmtp = new SmtpClient();
oSmtp.Authorized += OnAuthorized;
oSmtp.Connected += OnConnected;
oSmtp.Securing += OnSecuring;
oSmtp.SendingDataStream += OnSendingDataStream;
SmtpServer oServer = new SmtpServer(textServer.Text);
bool bSSL = (chkSSL.IsChecked.HasValue) ? (bool)chkSSL.IsChecked : false;
if (bSSL)
{
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
}
oServer.Protocol = (ServerProtocol)lstProtocols.SelectedIndex;
if (bAuth)
{
oServer.User = textUser.Text;
oServer.Password = textPassword.Password;
}
// For evaluation usage, please use "TryIt" as the license code, otherwise the
// "Invalid License Code" exception will be thrown. However, the trial object only can be used
// with developer license
// For licensed usage, please use your license code instead of "TryIt", then the object
// can used with published windows store application.
SmtpMail oMail = new SmtpMail("TryIt");
oMail.From = new MailAddress(textFrom.Text);
// If your Exchange Server is 2007 and used Exchange Web Service protocol, please add the following line;
// oMail.Headers.RemoveKey("From");
oMail.To = new AddressCollection(textTo.Text);
oMail.Cc = new AddressCollection(textCc.Text);
oMail.Subject = textSubject.Text;
if (lstFormat.SelectedIndex == 0)
{
oMail.TextBody = textBody.Text;
}
else
{
if (chkHtml.IsChecked.HasValue)
{
if ((bool)chkHtml.IsChecked)
{
editorMenu.Visibility = Windows.UI.Xaml.Visibility.Visible;
await htmlEditor.InvokeScriptAsync("OnViewHtmlSource", new string[] { "false" });
chkHtml.IsChecked = true;
}
}
string html = await htmlEditor.InvokeScriptAsync("getHtml", null);
html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>";
await oMail.ImportHtmlAsync(html,
Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures
| ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss);
}
int count = m_atts.Count;
for (int i = 0; i < count; i++)
{
await oMail.AddAttachmentAsync(m_atts[i]);
}
btnCancel.IsEnabled = true;
textStatus.Text = String.Format("Connecting {0} ...", oServer.Server);
// You can genereate a log file by the following code.
// oSmtp.LogFileName = "ms-appdata:///local/smtp.txt";
asyncCancel = oSmtp.SendMailAsync(oServer, oMail);
await asyncCancel;
textStatus.Text = "Completed";
}
catch (Exception ep)
{
textStatus.Text = "Error: " + ep.Message;
}
asyncCancel = null;
btnSend.IsEnabled = true;
btnCancel.IsEnabled = false;
}
示例4: SubmitMail
private async Task SubmitMail(
SmtpServer oServer, SmtpMail oMail, string[] atts,
string bodyText, bool htmlBody, int index )
{
SmtpClient oSmtp = null;
try
{
oSmtp = new SmtpClient();
// oSmtp.TaskCancellationToken = m_cts.Token;
// oSmtp.Authorized += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized);
oSmtp.Connected += OnConnected;
// oSmtp.Securing += new SmtpClient.OnSecuringEventHandler(OnSecuring);
//oSmtp.SendingDataStream +=
// new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream);
UpdateRecipientItem(index, "Preparing ...");
if ( !htmlBody )
{
oMail.TextBody = bodyText;
}
else
{
string html = bodyText;
html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>";
await oMail.ImportHtmlAsync(html,
Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures
| ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss);
}
int count = atts.Length;
for (int i = 0; i < count; i++)
{
await oMail.AddAttachmentAsync(atts[i]);
}
UpdateRecipientItem(index, String.Format("Connecting {0} ...", oServer.Server));
oSmtp.Tag = index;
// You can genereate a log file by the following code.
// oSmtp.LogFileName = "ms-appdata:///local/smtp.txt";
IAsyncAction asynCancelSend = oSmtp.SendMailAsync(oServer, oMail);
m_cts.Token.Register(() => asynCancelSend.Cancel());
await asynCancelSend;
Interlocked.Increment(ref m_success);
UpdateRecipientItem(index, "Completed");
}
catch (Exception ep)
{
oSmtp.Close();
string errDescription = ep.Message;
UpdateRecipientItem(index, errDescription);
Interlocked.Increment(ref m_failed);
}
}
示例5: TestCredentialsCopyInAsyncContext
public async void TestCredentialsCopyInAsyncContext()
{
SmtpServer server = new SmtpServer();
SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port);
MailMessage msg = new MailMessage("[email protected]", "[email protected]", "hello", "howdydoo");
CredentialCache cache = new CredentialCache();
cache.Add("localhost", server.EndPoint.Port, "NTLM", CredentialCache.DefaultNetworkCredentials);
client.Credentials = cache;
try
{
Thread t = new Thread(server.Run);
t.Start();
await client.SendMailAsync(msg);
t.Join();
Assert.Equal("<[email protected]>", server.MailFrom);
Assert.Equal("<[email protected]>", server.MailTo);
Assert.Equal("hello", server.Subject);
Assert.Equal("howdydoo", server.Body);
}
finally
{
server.Stop();
}
}