使用C#在asp.net中发送邮件的问题 [英] Problem in sending mail in asp.net with c#

查看:66
本文介绍了使用C#在asp.net中发送邮件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用此代码发送邮件,但是出现错误发送失败",
那么如何解决这个问题,我需要在web.config文件中进行哪些类型的更改

I have use this code for sending mail,but there is error "Failure to send",
so how to solve this Problem,what type of changes i have to do in web.config file

public static void SendMailMessage(string from, string to,string bcc, string cc, string subject, string body)
{
	MailMessage mMailMessage = new MailMessage();
	mMailMessage.From = new MailAddress(from );
  
		mMailMessage.To.Add(new MailAddress(to));
		if ((bcc != null) && (bcc != string.Empty))
		{
			mMailMessage.Bcc.Add(new MailAddress(bcc));
		}
  
	if ((cc != null) && (cc != string.Empty))
	{
		mMailMessage.CC.Add(new MailAddress(cc));
	}
	mMailMessage.Subject = subject;
	mMailMessage.Body = body;
	mMailMessage.IsBodyHtml = true;
	mMailMessage.Priority = MailPriority.Normal;
	SmtpClient mSmtpClient = new SmtpClient();
	mSmtpClient.Send(mMailMessage);
}

推荐答案

第一步,是否正确配置了在客户端上使用的电子邮件设置?
First step, are email settings correctly configured for use on the client?


您必须指定服务器和端口号:
You have to specify server and port number:
mSmtpClient.host = HostName;
mSmtpClient.port = PortNumber,



您可能还需要指定发件人电子邮件:



You may laso need to specify a From email:

MailMessage message = new MailMessage(FromEmail, ToEmail);




并且可能必须指定凭据:




and possibly have to specify Credentials:

mSmtpClient.Credentials = new System.Net.NetworkCredential(EmailUsername, EmailPassword);



可能还有SSL:



and possibly SSL:

mSmtpClient.EnableSsl = true;



我使用类似的代码连接到Hotmail,并且必须完成上述所有操作.代码如下所示:



I used similar code for connecting to Hotmail and had to do all of the above. The code looked like this:

public static void SendEmail(string Body, string UserEmail, string Subject)
    {
        SmtpClient client = new SmtpClient("smtp.live.com");
        MailMessage message = new MailMessage(Resources.Resource.OrganisationEmail, UserEmail);

        message.Body = Body;
        message.Subject = Resources.Resource.OrganisationName + Subject;

        client.Credentials = new System.Net.NetworkCredential(Resources.Resource.EmailUsername, Resources.Resource.EmailPassword);
        client.Port = 587;
        client.EnableSsl = true;
        client.Send(message);
    }



在这种情况下,我认为EmailUsername和OrganisationEmail相同,因为FromEmail必须与注册到Smpt Server帐户的电子邮件相同,但是使用Hotmail时,您的登录名就是您的电子邮件,因此它们是相同的.您的电子邮件密码就是您用来登录Hotmail的普通密码.



The EmailUsername and OrganisationEmail I believe were the same in this case as the FromEmail must be the same as the email registered to your Smpt Server Account but with Hotmail your login name is your email, thus they were the same. Your email password is just your normal password you use to login to Hotmail.


SmtpClient mSmtpClient = new SmtpClient()
mSmtpClient.Port = 25;
mSmtpClient.Host = "smtp.mail.yahoo.com";
mSmtpClient.Send(mMailMessage


用它来发送雅虎帐户


use it to send using yahoo account


这篇关于使用C#在asp.net中发送邮件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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