如何将字符串发送到指定的邮箱?

cybcha 2008-02-10 09:14:42
rt,在我的WinForm程序里面有一个字符串,我如何点击一个按钮就可以把这个字符串发送到我指定的邮箱中??
...全文
89 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnzdgs 2008-02-10
  • 打赏
  • 举报
回复
帮你贴上吧。
//---------------------------------------------------------------------
// This file is part of the Microsoft .NET Framework SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Net.Mail;

namespace Microsoft.Samples.Mailer
{

// Mailer sends an e-mail.
// It will authenticate using Windows authentication if the server
// (i.e. Exchange) requests it.
static class Mailer
{
enum MailMessagePart
{
From,
To,
Subject,
Message
}

static void Main(string[] args)
{
if (args.Length < 4)
{
Console.WriteLine(
"Expected: mailer.exe [from] [to] [subject] [message]");
return;
}

// Set mailServerName to be the name of the mail server
// you wish to use to deliver this message
string mailServerName = "smtphost";
string from = args[(int) MailMessagePart.From];
string to = args[(int) MailMessagePart.To];
string subject = args[(int) MailMessagePart.Subject];
string body = args[(int) MailMessagePart.Message];

try
{
// MailMessage is used to represent the e-mail being sent
using (MailMessage message =
new MailMessage(from, to, subject, body))
{

// SmtpClient is used to send the e-mail
SmtpClient mailClient = new SmtpClient(mailServerName);

// UseDefaultCredentials tells the mail client to use the
// Windows credentials of the account (i.e. user account)
// being used to run the application
mailClient.UseDefaultCredentials = true;

// Send delivers the message to the mail server
mailClient.Send(message);
}
Console.WriteLine("Message sent.");
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (SmtpException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
cnzdgs 2008-02-10
  • 打赏
  • 举报
回复
用SmtpClient类。
MSDN中有发邮件的例子代码,输入e-mail sample。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧