使用C#在asp.net中发送电子邮件时,如何设置web.config邮件设置或通过SMTP服务器 [英] How to set the web.config mail settings or through SMTP server when sending an email in asp.net using C#

查看:125
本文介绍了使用C#在asp.net中发送电子邮件时,如何设置web.config邮件设置或通过SMTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Default.aspx

This is my Default.aspx

<table>
    <tr>
    <td>
    Name
    </td>
    <td>
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    from
    </td>
    <td>
        <asp:TextBox ID="txtfrom" runat="server"></asp:TextBox>
    </td>
    </tr>

     <tr>
    <td>
    subject
    </td>
    <td>
        <asp:TextBox ID="txtsub" runat="server"></asp:TextBox>
    </td>
    </tr>
     <tr>
    <td>
    body
    </td>
    <td>
        <asp:TextBox ID="txtbody" runat="server"></asp:TextBox>
    </td>
    </tr>
     <tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </td>

    </tr>
    </table><>


Default.aspx.cs


Default.aspx.cs

using System.Net.Mail;
protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient mail = new SmtpClient();
        MailMessage msg = new MailMessage();
        string fromaddr = txtfrom.Text;
        msg.From = new MailAddress(fromaddr,txtname.Text);

        msg.To.Add("XXXXX@yahoo.com");
        msg.Subject = txtsub.Text;
        msg.IsBodyHtml = true;
        msg.Body = txtbody.Text;
        mail.Send(msg);
    }


错误:我得到的错误是不允许使用邮箱名称.服务器响应是:对不起,您的信封发件人在我的死信中,来自列表.


因此,请找到更好的解决方案,并向我发布有用的答案. 这非常紧急.


谢谢与问候

斯里坎特·J


整理好的代码块,紧急情况已删除:这可能对您来说很紧急,但对我们而言并非如此.您所强调的紧迫性只是使我们认为您离开得太晚了,并希望我们为您做这件事.这使某些人烦恼,并且可能减慢响应速度. -OriginalGriff [/edit]


Error: I am getting the error is Mailbox name not allowed. The server response was: Sorry, your envelope sender is in my badmailfrom list.


So, Please find a better solution and post a useful answer to me. this was an very urgent.


Thanks&Regards

Srikanth.J


[edit]Code blocks tidied up, urgency deleted: It may be urgent to you, but it isn''t to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response. - OriginalGriff[/edit]

推荐答案

您可以在Web.Config中配置SMTP配置,例如:
You can configure SMTP configuration in Web.Config like:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>



如果需要,请查看此Microsoft Video教程:
使用ASP.NET发送来自网站的电子邮件 [在ASP.NET中发送电子邮件的教程 [



If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
Tutorials on sending Email in ASP.NET[^]


如果您的错误是对不起,您的信封发件人在我的死信列表中."那么我建议您让用户在标有发件人"的文本框中键入不同的名称,并命名为"txtfrom".

这里有一个完整的通用例程,可以从ASP.NET发送电子邮件:
If your error is "Sorry, your envelope sender is in my badmailfrom list." then I would suggest you get your user to type something different into the textbox marked "From", and called "txtfrom".

There is a complete generic routine that sends email from ASP.NET here: Sending an Email in C# with or without attachments: generic routine.[^]


这篇关于使用C#在asp.net中发送电子邮件时,如何设置web.config邮件设置或通过SMTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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