您好我正在尝试在wpf应用中发送电子邮件但我卡住了; 我显示我的xaml代码
<Grid>
<Button Style="{DynamicResource ShowcaseRedBtn}" CommandParameter="test@ygmail.com" Tag="Send Email" Content="Button" Height="23" HorizontalAlignment="Left" Margin="351,186,0,0" Name="button1" VerticalAlignment="Top" Width="140" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="92,70,0,0" Name="txtSubject" VerticalAlignment="Top" Width="234" />
<TextBox AcceptsReturn="True" AcceptsTab="True" Height="159" HorizontalAlignment="Left" Margin="92,121,0,0" Name="txtBody" VerticalAlignment="Top" Width="234" />
</Grid>
并且在后面的代码中:
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn == null)
return;
string url = btn.CommandParameter as string;
if (String.IsNullOrEmpty(url))
return;
try
{
// here i wish set the parameters of email in this way
// 1. mailto = url;
// 2. subject = txtSubject.Text;
// 3. body = txtBody.Text;
Process.Start("mailto:test@gmail.com?subject=Software&body=test ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
我的目的是设置绑定表单中数据的电子邮件的参数: // 1. mailto = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;
你知道如何解决这一步吗?
非常感谢你的关注。
干杯
答案 0 :(得分:11)
您可以使用System.Net.MailMessage类直接发送邮件。请查看此类的MSDN文档中的以下示例:
public static void CreateTimeoutTestMessage(string server)
{
string to = "jane@contoso.com";
string from = "ben@contoso.com";
string subject = "Using the new SMTP client.";
string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
client.Timeout = 100;
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTimeoutTestMessage(): {0}",
ex.ToString() );
}
}
答案 1 :(得分:1)
如果您使用的是代码隐藏,则不需要绑定 - 虽然它不是很好的模式,但它当然会有效。
为什么不直接命名文本框(urlTextBox,subjectTextBox等)并在按钮点击事件中使用这些名称?
Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", urlTextBox.Text, subjectTextBox.Text, ... ));
当然,如果用户输入无效值,这可能很容易失败。
使用绑定是另一种方法,但在这个简单的情况下,我认为它是开销。
答案 2 :(得分:1)
我发布了一个类似且更简单的答案here
但重点是
<Button Content="Send Email" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:LaunchUriOrFileAction Path="mailto:example@stackoverflow.com?subject=SubjectExample" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
&#13;