如何在C#中发送电子邮件到Outlook 2010?

3

即使我的服务器没有安装Outlook 2010且位于内部网络区域,我是否可以使用Outlook发送电子邮件?因为每个人都使用Outlook进行通信,并拥有唯一的Outlook帐户。我该如何从我的应用程序发送电子邮件?我很确定我无法使用以下代码来解决我的问题,请有人帮助我。

代码:

// 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;

有任何答案帮到了您吗?请标记为答案。 - Rob
3个回答

6

这是我在自己的项目中使用的示例代码:

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

在这个例子中,我包含了使用文件附件所需的一切。

1

如果您想发送电子邮件,您不需要Outlook,而是需要电子邮件地址,例如:

MailMessage mail = new MailMessage();
mail.From = ""; // put the from address here
mail.To = ""; // put to address here
mail.Subject = ""; // put subject here
mail.Body = ""; // put body of email here
SmtpMail.SmtpServer = ""; // put smtp server you will use here
// and then send the mail
SmtpMail.Send(mail);

并阅读此发送电子邮件

编辑

但如果您想使用OUTLOOK,请尝试这个

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application oApp = new Outlook.Application();

                    Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));
                    email.Recipients.Add("EmailAddress@google.com");
                    email.Subject = "Subject";
                    email.Body = "Message";


                    ((Outlook.MailItem)email).Send();

编辑2

第一段代码示例

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.From = new System.Net.Mail.MailAddress("mymail@gmail.com");//who send
mm.To.Add(new System.Net.Mail.MailAddress("Yourmail@gmail.com"));//where send
mm.Subject = "Subj";
mm.Body = "text";
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("127.0.0.1");
client.Send(mm);

我遇到了COM异常..这是否意味着你的代码中任何地址都可以使用? - user1037134
两个都试了,但第一个给了我一个连接错误,说无法连接到服务器。 - user1037134
@HuatsinYeo 你需要阅读这篇文章以使用编辑部分 http://www.c-sharpcorner.com/UploadFile/casperboekhoudt/SendingEmailsThroughOutlook12052005000124AM/SendingEmailsThroughOutlook.aspx - Likurg
@HuatsinYeo和我需要看到异常,以便寻求帮助。 - Likurg

0
在ASP.NET中,您还可以通过脚本管理器使用用户的Outlook发送电子邮件:
//add this to your page body (or in master page body)
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>

//add this to your SendEmailButton_Click() event
string email = "mailto:recipient@domain.com?subject=my subject&body=my message.";
StringBuilder sb = new StringBuilder();
sb.Append("document.location.href='" + email + "';");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "sendemail", sb.ToString(), true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接