如何将电子邮件Outlook功能添加到我的桌面C#应用程序? [英] How Can I Add email outlook function to my desktop C# Application?

查看:71
本文介绍了如何将电子邮件Outlook功能添加到我的桌面C#应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个桌面应用程序,它也有一个电子邮件功能,但它会给我一个错误,我无法弄清楚它。



System.Net.Mail.SmtpException:发送邮件失败--->

System.Net.WebException:无法连接到远程服务器--->

system.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应74.125.68.109:587

在System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)



在System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,套接字和套接字,IPAddress&地址,ConnectSocketState状态,IAsyncResult asyncResult,异常和异常)

---内部异常堆栈跟踪结束---



nt.GetConnection(PooledStream PooledSteam,Object owner,Boolean async,IPAddress&地址,插座& adobrtSocket,Socket& System.Net.PooledStream.Active(Object owningObject,Boolean async,GeneralAsyncDelegate asyncCallback)
$ / b $ / b $ $ $ $ $ $ $ $ $ $ $ $ b $ .GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32创建超时)

在System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)

在System.Net.Mail。 SmtpClient.Send(MailMessage消息)

---内部异常堆栈跟踪结束---



在System.Net.Mail。 smtpClient.Send(MailMessage消息)

at EnQApp.Mailer.SendBtn_Click(Object sender,EventArgs e)

c:\ Users \ businesssupport \ Document'Visual工作室

2012 \Projects\EnQApp\Mailer.cs:第62行





这是我的代码:



I have created a desktop application and it has a email function as well, but it'll give me an error which i can't figure out about it.

System.Net.Mail.SmtpException: Failure sending mail --->
System.Net.WebException: Unable to connect to the remote server --->
system.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after period of time, or established connection failed because connected host has failed to respond 74.125.68.109:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4, Socket s6,Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---

at System.Net.ServicePoint.GetConnection(PooledStream PooledSteam, Object owner, Boolean async, IPAddress& address, Socket& adobrtSocket, Socket& abortSocket6)

at System.Net.PooledStream.Active(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creation Timeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---

at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EnQApp.Mailer.SendBtn_Click(Object sender, EventArgs e) in
c:\Users\businesssupport\Document\Visual Studio
2012\Projects\EnQApp\Mailer.cs:line 62


this is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;

namespace EnQApp
{
    public partial class Mailer : Form
    {
        String path;
        MailMessage mail = new MailMessage();
        public Mailer()
        {
            InitializeComponent();
        }

        private void Mailer_Load(object sender, EventArgs e)
        {
            fromTxtBox.Text = "hemasenquiries@gmail.com";
        }

        private void sendBtn_Click(object sender, EventArgs e)
        {
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
            SmtpServer.Credentials = new System.Net.NetworkCredential
                                   ("xyz@gmail.com", "xyz@123");
            SmtpServer.EnableSsl = true;
            mail = new MailMessage();
            String[] addr = toTxtBox.Text.Split(',');
            try
            {
                mail.From = new MailAddress("hemasenquiries@gmail.com",
                "Developers", System.Text.Encoding.UTF8);
                Byte i;
                for (i = 0; i < addr.Length; i++)
                    mail.To.Add(addr[i]);
                mail.Subject = subjectTxtBox.Text;
                mail.Body = bodyTxtBox.Text;
                if (AttachementsLB.Items.Count != 0)
                {
                    for (i = 0; i < AttachementsLB.Items.Count; i++)
                        mail.Attachments.Add(new Attachment(AttachementsLB.Items[i].ToString()));
                }
                LinkedResource logo = new LinkedResource(path);
                logo.ContentId = "Logo";
                string htmlview;
                htmlview = "<html><body><table border=2><tr width=100%><td><img src=cid:Logo alt=companyname /></td><td>MY COMPANY DESCRIPTION</td></tr></table><hr/></body></html>";
                AlternateView alternateView1 = AlternateView.CreateAlternateViewFromString
                          (htmlview + bodyTxtBox.Text, null, MediaTypeNames.Text.Html);
                alternateView1.LinkedResources.Add(logo);
                mail.AlternateViews.Add(alternateView1);
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress(toTxtBox.Text);
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                AttachementsLB.Items.Add(openFileDialog1.FileName);
            }
        }

        private void attachBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog d1 = new OpenFileDialog();
            if (d1.ShowDialog() == DialogResult.OK)
            {
                path = d1.FileName;
                bodyTxtBox.Text = d1.FileName;
            }
        }
    }
}

推荐答案

//this code works
//screen on http://s8.postimg.org/78uwhraz9/Capture.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
            SmtpServer.Credentials = new System.Net.NetworkCredential
                                   ("sender@gmail.com", "sendersPassword");
            SmtpServer.EnableSsl = true;
            var mail = new MailMessage();
            String[] addr = "agnaldo@50minutos.com.br".Split(',');
            try
            {
                mail.From = new MailAddress("sender@gmail.com",
                "Developers", System.Text.Encoding.UTF8);
                Byte i;
                for (i = 0; i < addr.Length; i++)
                    mail.To.Add(addr[i]);
                mail.Subject = "assunto";
                mail.Body = "mensagem";
                            mail.Attachments.Add(new Attachment(@"z:\sql\bkp.bak"));
                            LinkedResource logo = new LinkedResource(@"z:\sql\x.png");
                logo.ContentId = "Logo";
                string htmlview;
                htmlview = "<html><body><table border="2"><tr width="100%"><td><img src="cid:Logo" alt="companyname" /></td><td>MY COMPANY DESCRIPTION</td></tr></table><hr /></body></html>";
                AlternateView alternateView1 = AlternateView.CreateAlternateViewFromString
                          (htmlview + "mensagem", null, MediaTypeNames.Text.Html);
                alternateView1.LinkedResources.Add(logo);
                mail.AlternateViews.Add(alternateView1);
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
               Console.WriteLine(ex.ToString());
            }

            Console.ReadKey();
        }
    }
}


这篇关于如何将电子邮件Outlook功能添加到我的桌面C#应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆