3

サーバーがOutlook2010とそのイントラネットゾーン内にインストールされていないにもかかわらず、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;
4

3 に答える 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!");
    }
  }
}

この例では、添付ファイルを使用するために必要なものをすべて含めました。

于 2012-10-01T21:29:02.147 に答える
1

あなたが電子メールを送りたいなら、あなたは見通しを必要としませんでした、あなたは例えば電子メールアドレスを必要とします

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);
于 2012-05-03T08:43:38.817 に答える
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);
于 2012-12-20T22:54:51.113 に答える