我正在使用我必须在我的 Windows 8 桌面应用程序中安装和引用的EASendMail SMTP 组件。我正在使用它来发送包含一些 html 内容的电子邮件。
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
btnSend.IsEnabled = false;
await Send_Email();
btnSend.IsEnabled = true;
}
private async Task Send_Email()
{
var usermail = email_txt.ToString();
String Result = "";
try
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set sender email address, please change it to yours
oMail.From = new MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
// oMail.To.Add(new MailAddress("support@emailarchitect.net"));
oMail.To.Add(new MailAddress(usermail));
// Set email subject
oMail.Subject = "test email from C# XAML project with file attachment";
// Set Html body
oMail.HtmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>";
// get a file path from PicturesLibrary,
// to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
// your project -> Package.appxmanifest -> Capabilities
Windows.Storage.StorageFile file =
await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("test.jpg");
string attfile = file.Path;
Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);
// if you want to add attachment from remote URL instead of local file.
// string attfile = "http://www.emailarchitect.net/test.jpg";
// Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);
// you can change the Attachment name by
// oAttachment.Name = "mytest.jpg";
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";
// If your SMTP server requires TLS connection on 25 port, please add this line
// oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// If your SMTP server requires SSL connection on 465 port, please add this line
// oServer.Port = 465;
// 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();
}
当单击按钮时,上述内容应将电子邮件发送到在 XAML 页面上的 TextBox 中输入的电子邮件地址
<TextBox x:Name="email_txt"></TextBox>
<Button x:Name="email_btn" Content="Emial Me" Click="email_btn_Click"/>
email_txt
代码应将电子邮件发送到单击按钮时输入的任何电子邮件地址email_btn
。
为此,我将文本框值放在一个变量中var usermail = email_txt.ToString();
,并usermail
在oMail.To.Add(new MailAddress(usermail));
. 但是有了这个我得到下面的错误:
它无法识别 中的电子邮件地址usermail
。
但是,如果我直接在代码中输入电子邮件地址,例如oMail.To.Add(new MailAddress("support@emailarchitect.net"));
,它可以正常工作,并且电子邮件会发送到 support@emailarchitect.net。
如何解决此问题,以便将电子邮件发送到 TextBlock 中指定的地址?