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

本文关键字:2010 电子邮件 Outlook 中向 | 更新日期: 2023-09-27 18:32:56

尽管我的服务器没有安装 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;

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

这是我在项目中使用的示例片段:

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

在此示例中,我包含了使用文件附件所需的所有内容。

如果你想发送电子邮件,你不需要Outlook,你需要电子邮件adres,例如

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

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