Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用c#向outlook 2010发送电子邮件?_C#_Asp.net_Visual Studio 2010 - Fatal编程技术网

如何用c#向outlook 2010发送电子邮件?

如何用c#向outlook 2010发送电子邮件?,c#,asp.net,visual-studio-2010,C#,Asp.net,Visual Studio 2010,尽管我的服务器未在intranet区域内安装outlook 2010及其应用程序,但是否可以使用outlook发送电子邮件?因为这里的每个人都与outlook进行通信,并且拥有唯一的outlook帐户。如何从我的应用程序发送电子邮件?我很确定我不能用下面的代码来解决我的问题,请有人帮助我 代码: 例如,如果你想发送电子邮件,你不需要outlook,你需要电子邮件地址 MailMessage mail = new MailMessage(); mail.From = ""; // put the

尽管我的服务器未在intranet区域内安装outlook 2010及其应用程序,但是否可以使用outlook发送电子邮件?因为这里的每个人都与outlook进行通信,并且拥有唯一的outlook帐户。如何从我的应用程序发送电子邮件?我很确定我不能用下面的代码来解决我的问题,请有人帮助我

代码:


例如,如果你想发送电子邮件,你不需要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();
EDIT2

第一个代码示例

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

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

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

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

在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);
//将其添加到页面正文(或母版页正文)
//将此添加到SendEmailButton\u Click()事件
string email=“mailto:recipient@domain.com“主题=我的主题&正文=我的信息。”;
StringBuilder sb=新的StringBuilder();
sb.Append(“document.location.href=”+email+“;”);
ScriptManager.RegisterStartupScript(this.Page,this.GetType(),“sendmail”,sb.ToString(),true);

我有COM异常..这是否意味着任何地址都可以在您的代码中工作?两者都有,但首先给我一个连接错误,说它无法连接到服务器。@HuatsinYeo您需要阅读此内容以供使用编辑部分@HuatsinYeo,我需要查看exeption,以获取帮助有任何答案可以帮助您吗?请注明答案。
//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);