c#asp 4.5中的emai代码项目 [英] code project of emai in c# asp 4.5

查看:140
本文介绍了c#asp 4.5中的emai代码项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net发送电子邮件



错误是

邮件无法发送到SMTP服务器。传输错误代码是0x80040217。服务器响应不可用



和代码是



How to email in asp.net

it error is
The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available

and code is

using System.Web.Mail;
using System;
public class MailSender
{
    public static bool SendEmail(
        string pGmailEmail, 
        string pGmailPassword, 
        string pTo, 
        string pSubject,
        string pBody, 
        System.Web.Mail.MailFormat pFormat,
        string pAttachmentPath)
    {
        
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");
            //sendusing: cdoSendUsingPort, value 2, for sending the message using 
            //the network.

            //smtpauthenticate: Specifies the mechanism used when authenticating 
            //to an SMTP 
            //service over the network. Possible values are:
            //- cdoAnonymous, value 0. Do not authenticate.
            //- cdoBasic, value 1. Use basic clear-text authentication. 
            //When using this option you have to provide the user name and password 
            //through the sendusername and sendpassword fields.
            //- cdoNTLM, value 2. The current process security context is used to 
            // authenticate with the service.
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;
            if (pAttachmentPath.Trim() != "")
            {
                MailAttachment MyAttachment = 
                        new MailAttachment(pAttachmentPath);
                myMail.Attachments.Add(MyAttachment);
                myMail.Priority = System.Web.Mail.MailPriority.High;
            }

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        
    }
}







===== ========================================




=============================================

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailSender.SendEmail(txtGmailId.Text + "@gmail.com", txtPassword.Text, txtTo.Text, txtSubject.Text, txtMessage.Text, System.Web.Mail.MailFormat.Text, "");
            lblError.Text = "Mail sent successfully.";
        }
        catch(Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}

推荐答案

System.Web.Mail 已过时。使用 System.Net.Mail [< a href =http://msdn.microsoft.com/en-us/library/system.net.mail%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ <相反。



http://www.systemnetmail.com/ [ ^ ]
System.Web.Mail is obsolete. Use System.Net.Mail[^] instead.

http://www.systemnetmail.com/[^]


使用System.Net.Mail,而不是已弃用的System.Web.Mail



http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail [ ^ ]
use System.Net.Mail, not the deprecated System.Web.Mail

http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail[^]


你得到的错误表示SMTP服务器拒绝了您的帐户。原因可能是:

1.错误的凭据(用户名/密码)

2.帐户因某些原因被禁用

3. SMTP配置为匿名访问,在这种情况下,您不应该提供所有凭据
The error you got means that SMTP server rejected your account. The reasons can be:
1. Wrong credentials (user name/password)
2. Account is disabled for some reasons
3. SMTP is configured to anonymous access, in which case you should not provide credentials at all


这篇关于c#asp 4.5中的emai代码项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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