Use .NET send email:
If you want to launch the default mail client to create a new email message, it's really simple and there's plenty of samples available:
System.Diagnostics.Process.Start( "mailto:user@example.com?subject=TestSubject&body=TestBody" );
But what if you just want to launch the client and nothing more? You'll have to read the registry (remember that you need the proper RegistryPermission for this) to find the default mail client and launch that executable yourself. Here's a method that will do just that:
/// <summary>
/// Launches the default mail client.
/// </summary>
public void LaunchDefaultMailClient()
{
// Open the "HKLM/SOFTWARE/Clients/Mail" key.
Microsoft.Win32.RegistryKey mailKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( @"SOFTWARE/Clients/Mail" );
// The default mail application is stored in the default value of that key.
string defaultMailApp = (string)mailKey.GetValue( null );
if( defaultMailApp != null && defaultMailApp.Length > 0 )
{
// Open the subkey of the default mail application and the "shell/open/command" key below that.
Microsoft.Win32.RegistryKey cmdKey = mailKey.OpenSubKey( defaultMailApp + @"/shell/open/command" );
// We're now in "HKLM/SOFTWARE/Clients/Mail/<default-mail-app>/shell/open/command".
// The default value of this key is the command line to start.
string command = (string)cmdKey.GetValue( null );
// If there are command arguments, extract them out of the main command string.
string args = string.Empty;
if( command.IndexOf( " " ) > 0 )
{
args = command.Substring( command.IndexOf( " " ) + 1 );
command = command.Substring( 0, command.IndexOf( " " ) );
}
// Start a new process for the mail application.
System.Diagnostics.Process.Start( command, args );
}
}
启动默认邮件客户端
本文介绍了一种通过读取注册表来查找并启动默认邮件客户端的方法,避免直接打开带有预设内容的新邮件。此方法适用于需要仅启动邮件应用程序而不发送特定邮件的情况。
3374

被折叠的 条评论
为什么被折叠?



