无法发送电子邮件 [英] could not send an email message
本文介绍了无法发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hellow
i尝试了很多,但以下代码从不向任何一个地址发送任何电子邮件消息,它没有错误!,请帮助
Hellow
i have tryed alot but the following code never sends any Email message to either address,it has no error!,please help
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;// for MediaTypeNames
using System.Net;//CredentialCache
using System.Collections;
//http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
//http://www.codeproject.com/Articles/13236/Sending-complex-emails-in-NET-1-1
namespace Email1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Send_Click(object sender, EventArgs e)
{ string Myfile = "Abdk.wma";// <----- window media audio file
MailAddress from = new MailAddress("ksabtank@hotmail.com", "yahoo");
MailAddress to = new MailAddress("qazwsxkhalid@yahoo.com", "hotm");
MailMessage message = new MailMessage(from,to);//, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
message.Subject = "Issue Report";
message.Body = "Submitted By: ";// +txtName.Text + "\n";
message.Body += "my comment";
SmtpClient SmtpMail = new SmtpClient();//localhost);//smtp.server.com);//server);
//SmtpMail.UseDefaultCredentials = true;
//SmtpMail.Credentials = CredentialCache.DefaultNetworkCredentials;
//SmtpMail.Credentials = new System.Net.NetworkCredential(str1, str2);//
//CredentialCache cache = new CredentialCache();
//SmtpMail.Host = "smtp.gmail.com";
SmtpMail.Host = "smtp.hotmail.com";
//SmtpMail.Host = "smtp.yahoo.com";
SmtpMail.Port = 25;
SmtpMail.Timeout = 50000;
SmtpMail.EnableSsl = true;
Attachment data = new Attachment(Myfile, MediaTypeNames.Application.Octet);//this is the file "Abdk.wma"
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(Myfile);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(Myfile);
disposition.ReadDate = System.IO.File.GetLastAccessTime(Myfile);
message.Attachments.Add(data);
try {
SmtpMail.Send(message);
}
catch (Exception ex){
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString() );
}
}
}
}
推荐答案
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
这篇关于无法发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文