首页 > 解决方案 > 将带有附件的信息发送到一个电子邮件地址,尤其是使用谷歌

问题描述

我想在单击按钮时发送一封带有附件的电子邮件,但即使我已连接到互联网,它仍显示“发送邮件失败”我仍然无法弄清楚问题是什么

private void proto_Type_AI_(object sender, RoutedEventArgs e)
        {           
            try
            {
                //Smpt Client Details                    
                SmtpClient clientDetails = new SmtpClient();
                clientDetails.Port = Convert.ToInt32(port_number.Text.Trim());
                clientDetails.Host = smtp_server.Text.Trim();
                clientDetails.EnableSsl = ssl.IsChecked == true;
                clientDetails.DeliveryMethod = SmtpDeliveryMethod.Network;
                clientDetails.UseDefaultCredentials = false;
                clientDetails.Credentials = new NetworkCredential(sender_email.Text.Trim(), sender_password.Password.Trim());

                //message details
                MailMessage maildetails = new MailMessage();
                maildetails.From = new MailAddress(sender_email.Text.Trim());
                maildetails.To.Add(recipent_email.Text.Trim());
                //maildetails.Bcc.Add("bcc email address");
                maildetails.Subject = subject.Text.Trim();
                maildetails.IsBodyHtml = html.IsChecked == true;
                maildetails.Body = body.Text.Trim();               

                clientDetails.Send(maildetails);

                MessageBox.Show("HEY USER, YOUR MESSAGE HAS BEEN SENT");

            }
            catch (Exception ex)
            {[![enter image description here][1]][1]
                MessageBox.Show(ex.Message);
            }
        }

以下是我使用google smtp发送的输出图片

标签: c#wpfemail

解决方案


using Microsoft.Office.Interop.Outlook; 

namespace YourNamespace
{
   public class YourClass
   { 
    //Create a mail object 
    Microsoft.Office.Interop.Outlook.Application ol = new  Microsoft.Office.Interop.Outlook.Application(); 
    //Create a mail item 
    Microsoft.Office.Interop.Outlook.MailItem mailitem = ol.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
    //Your mail subject text 
    mailitem.Subject = "SMART G's Auto Email Test"; 
    //Mail Recipient 
    mailitem.To = "smartg@smartg.com"; 
    //Mail Sub Recipient 
    mailitem.CC = "test@test.com"; 
    //Your E-Mail Text 
    mailitem.Body = "Dear Sir or Madam,\n\n" + 
   "Attached please find my great manual on how to save the world. Thank you for reading. + "\n\n"
    + "Kind regards" + "\n\n" + "Smart G"; 
    //Your mail attachment
    mailitem.Attachments.Add(<<path>> + <<documentName>>); 
    //Do you want me to show you the mail?                           
    mailitem.Display(true); //yes 
    //If you want to check the mail before sending it then do not use this.     
    mailitem.Send(); //automatically sends the mail before you can check it! (autosend) 
    //Notifcation 
    MessageBox.Show("Mail sent!")
    }
}

推荐阅读