我需要一些.NET (C#)和方面的帮助。我正在构建一个简单的桌面应用程序,并希望使用Outlook发送电子邮件。
我使用VS 2008专业版和C#,目标是.NET 3.5
任何帮助,示例代码都是非常感谢的。
发布于 2008-12-29 00:23:36
此代码是直接从MSDN示例中采用的。
using System.Net;
using System.Net.Mime;
using System.Net.Mail;
...
...
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent
string file = @"C:\Temp\data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"from@gmail.com",
"to@gmail.com",
"Subject: Email message with attachment.",
"Body: See the attached spreadsheet.");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment...
message.Attachments.Add(data);
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("CreateMessageWithAttachment() Exception: {0}",
ex.ToString());
throw;
}
}
发布于 2008-12-28 11:26:39
使用MAPI,p/invoke接口使使用类似于来自MailItem.Send法的C#的东西成为可能。mapi32.MAPISendMail页面提供了设置接口的示例:
/// <summary>
/// The MAPISendMail function sends a message.
///
/// This function differs from the MAPISendDocuments function in that it allows greater
/// flexibility in message generation.
/// </summary>
[DllImport("MAPI32.DLL", CharSet=CharSet.Ansi)]
public static extern uint MAPISendMail(IntPtr lhSession, IntPtr ulUIParam,
MapiMessage lpMessage, uint flFlags, uint ulReserved);
相同的p/invoke页面还提供了一个警告:注意它!托管代码不支持MAPI32。
您应该考虑使用本机System.Net.Mail.SmtpClient类通过SMTP而不是outlook发送邮件。
发布于 2008-12-28 23:39:43
我强烈建议使用救赎。它有一个非常容易使用,易学的API,它可以做的不仅仅是发送电子邮件。
https://stackoverflow.com/questions/396069
复制相似问题