如何使用C#向Outlook 2010发送电子邮件? [英] How do i send email to outlook 2010 in c#?
本文介绍了如何使用C#向Outlook 2010发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尽管我的服务器未在企业内部网区域中安装Outlook 2010及其Outlook,但是否仍可以通过Outlook发送电子邮件?因为这里的每个人都与Outlook进行交流,并且拥有唯一的Outlook帐户.如何从我的应用程序发送电子邮件?我很确定我不能使用以下代码来解决我的问题,请帮助我.
Is it possible to send email with outlook despite my server doesnt install outlook 2010 and its within the intranet zone? Because everyone here communicate with outlook and had a unique outlook account. How can i send email from my application? I am pretty sure i cannt use the following code to solve my problem, someone please help me.
代码:
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.HTMLBody = body;
//Add an attachment.
//String sDisplayName = "MyAttachment";
///int iPosition = (int)oMsg.Body.Length + 1;
//int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//now attached the file
//Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
//Subject line
oMsg.Subject = subject;
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(address);
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
推荐答案
这是我在我的项目中使用的示例代码段:
This is an example snippet I used in a project of mine:
using System.Net.Mail;
using System.Net;
private void SendMail( string targetMail,
string shownTargetName,
string[] attachmentNames) {
var fromAddress = new MailAddress("support@infinibrain.net", "MailSendingProgram");
var toAddress = new MailAddress(targetMail, shownTargetName);
const string fromPassword = "12345isAbadPassword";
subject = "Your Subject";
body =
@"
Here
you can put in any text
that will appear in the body
";
var smtp = new SmtpClient {
Host = "smtp.mailserver.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress) {
Subject = subject,
Body = body }
) {
foreach(string filePath in attachmentNames[]) {
Attachment attachMail = new Attachment(filePath);
message.Attachments.Add(attachMail);
}
try {
smtp.Send(message);
MessageBox.Show("E-Mail sent!");
} catch {
MessageBox.Show("Sending failed, check your internet connection!");
}
}
}
在此示例中,我包括了使用文件附件所需的所有内容.
In this example I included everything required to use file attachments.
这篇关于如何使用C#向Outlook 2010发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文