创建和发送Outlook电子邮件

时间:2013-03-03 04:18:35

标签: c# email outlook

我一直在漫游论坛而没有找到我的问题的任何答案。 所有解决方案(和问题)都是关于使用Microsoft.Office.Interop.Outlook; 出于某种原因我不允许使用任何 office.interop

我甚至尝试过:

MailAddress fromAddress = new MailAddress("sender@server.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = fromAddress;
message.To.Add("receiver@theirServer.com");
message.CC.Add("receiver2@theirServer.com");
message.Subject = "theSubject";
message.Body = "TheBody";

SmtpClient smtpClient = new SmtpClient("zzz.server.xxx");
smtpClient.Credentials = new NetworkCredential("sender@server.com", "password");

smtpClient.Send(message);

即使我对密码进行了硬编码,代码也无法对凭证进行身份验证,但我觉得必须有更好的方法。

3 个答案:

答案 0 :(得分:2)

您的问题有很多不足之处。您可以发布服务器的响应或您收到的错误吗?

以下是一些可能对您有所帮助的观察和反馈 指定端口

SmtpClient smtpClient = new SmtpClient("zzz.server.xxx", PORTNUMBER);

设置一些基本属性

smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false; //must be set prior to credentials
smtpClient.Credentials = new NetworkCredential("username", "pass");

设置消息编码

message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;

发送Asyncronously并使用回调来确定结果

//add callback 
smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

//set token
string userToken = "tokenString";

//send asynchronously
smtpCient.SendAsync(message, userToken);

public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
             String token = (string) e.UserState;

            if (e.Cancelled)
            {
                 //do something if it was cancelled
            }
            if (e.Error != null)
            {
               MessageBox.Show( e.Error.ToString());
            } else
            {
                MessageBox.Show("Message sent.");
            }
        }

答案 1 :(得分:0)

您是否通过在其他客户端(如Outlook)中配置帐户来验证凭据,包括用户名的格式?

您可能需要:

smtpClient.Credentials = new NetworkCredential("sender", "password");

smtpClient.Credentials = new NetworkCredential("DOMAIN\sender", "password");

答案 2 :(得分:0)

您的电子邮件服务器是Exchange Server吗?如果是这样,您可以使用Exchange Web服务(EWS)发送电子邮件并将副本保存在“已发送邮件”文件夹中。这里有一个简单的例子:

Send Exchange Email

More Sample Code