使用.NET技术发送邮件(通过调用Outlook)

本文介绍了一种通过读取注册表来查找并启动默认邮件客户端的方法,避免直接打开带有预设内容的新邮件。此方法适用于需要仅启动邮件应用程序而不发送特定邮件的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 );
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值