从我的网站发送电子邮件 [英] send email from my website

查看:188
本文介绍了从我的网站发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释了如何在不同的站点中发送电子邮件,但是都没有用.有些复杂,有些却不能正常工作.
我的网站需要发送电子邮件给其他人.
以下是我的资料,但是此代码无法正常工作.为什么?

Explained how to send email in different sites, but neither worked., some complex and some just did not work properly.
My website needs to send email to other.
Below is my source, But this code does not work. why?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class SendDummyMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SmtpClient ob = new SmtpClient();
            MailMessage obMsg = new MailMessage();
            obMsg.To.Add(new MailAddress(to@abcd.com);
            obMsg.Subject = "Please Add subject";
            obMsg.Body = "this is  a test";
            obMsg.IsBodyHtml = true;            
            ob.Send(obMsg);

        }

        catch (Exception e1)
        {
            Response.Write(e1.Message);
        }

    }
}



web.config文件



web.config file

<system.net>
        <mailSettings>
            <smtp from="" >
                <network host="SmtpServer" port="25" defaultCredentials="true"/>
            </smtp>
        </mailSettings>
    </system.net>

推荐答案

要做的第一件事是更正主机名.显然,这不是真实的SMTP服务器名称.如果您的网络中有SMTP服务器,请与管理员联系以获取服务器名称或IP地址.

第二件事是,如果SMTP服务器需要身份验证,则应该定义凭据或使用允许与SMTP服务器通信的凭据运行该过程.第一种选择很典型.

如果您的网络中没有SMTP服务器,请考虑使用外部电子邮件系统(例如gmail).
The first thing to do is to correct the host name. It''s obviously not a real SMTP server name. If you have a SMTP server in your network, talk to the administrator to get the server name or the IP address.

Second thing is that if the SMTP server is expecting authentication, you should either define the credentials or run the process with credentials that are allowed to communicate with the SMTP server. The first option is quite typical one.

If you don''t have a SMTP server in your network, consider using outside email system such as gmail.


可能您需要授权才能发送电子邮件.在这里看看-有一个工作代码示例,非常易于粘贴和使用:
Probably, you need authorization to send the email. Have a look here - there is a working code example which is pretty simple to paste and use: Sending an Email in C# with or without attachments: generic routine.[^]


我会注册类似SendGrid.com并通过它们发送电子邮件.减轻从网络发送电子邮件的负担.

这是我将使用的代码示例:
I would sign up to a service like SendGrid.com and send emails through them. Keep the burden of sending emails off your network.

Here is an example of the code i would use:
var smtp = new SmtpClient
{
Host = "smtp.sendgrid.net",
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Port = 25,
Credentials = new NetworkCredential("name@example.com", "password")
};
MailMessage thisMessage = new MailMessage(fromAddress, toAddress);
thisMessage.IsBodyHtml = true;
thisMessage.Subject = "Title Here";
thisMessage.Body = "Sample Body";
foreach (DataRow thisEmail in dtEmails.Rows)
{
thisMessage.Bcc.Add(thisEmail["AccountEmail"].ToString());
if (thisMessage.Bcc.Count > 500)
{
smtp.Send(thisMessage);
thisMessage.Bcc.Clear();
}
}
smtp.Send(thisMessage);


这篇关于从我的网站发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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