从网站发送邮件 [英] Sending mail from website

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

问题描述

大家好..

我写了一段代码,用于从网页发送电子邮件到gmail id。以下是完整的CS页面:



Hi guys..
I have a piece of code written to send e-mail from webpage to a gmail id. Below is the full CS page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Contact_Us : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {            
                    sendmail();
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=JavaScript>alert(''Query sent successfully.'');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    TextBox3.Text = "";
                    TextBox4.Text = "";                
        }
        catch(Exception)
        {

        }
    }

    void sendmail()
    {
        string mail_text = TextBox3.Text + "\n\nFrom: " + TextBox1.Text + "\n\nE-Mail: " + TextBox2.Text;
        MailMessage message = new MailMessage("id@gmail.com", "id@gmail.com", TextBox4.Text, mail_text);
        SmtpClient clnt = new SmtpClient();
        clnt.Port = 587;
        clnt.Host = "smtp.gmail.com";
        clnt.Credentials = new System.Net.NetworkCredential("id@gmail.com", "password");
        clnt.EnableSsl = true;
        clnt.Send(message);
    }
}





现在我面临的问题是,当我在本地运行此代码时服务器通过visual studio,它工作正常,邮件发送。但是,当我发布该网站并尝试从Web服务器发送邮件时,邮件没有被发送。文本框也没有变成空白,所以我的猜测是代码抛出一些异常并转到Catch部分。问题是什么?我需要改变什么?请帮助..

谢谢:)



Now the problem that I am facing is, when i run this code in my local server through visual studio, it works fine and the mail gets sent. But when i published the site and is trying to send mail from the web server, the mail is not getting sent. Also the textboxes are not getting blank, so my guess is the code is throwing some exception and is going over to the Catch section. What is the issue and what do i need to change?? Please help..
Thanks :)

推荐答案

我们不能告诉你什么。



你需要做的第一件事是停止吞下所有异常 - 它们是有原因的,如果你只是茫然吞下它而不查看或记录异常的事实,你永远无法弄清楚问题是什么。



所以得到异常,并将其记录到文件,数据库,或任何你已经有了,所以你可以查看内容并找出问题所给出的关于它失败原因的实际信息!
We can''t tell from that any more than you can.

The first thing you need to do is stop swallowing all exceptions - they are there for a reason, and if you just blankly swallow it without even looking or recording the fact of an exception, you will never be able to work out what the heck the problem is.

So get the exception, and log it to a file, or a database, or whatever you have available, so you can look at the content and work out what the problem is given some actual information on why it failed!


15年前你可能没有任何障碍就可以做到这一点。垃圾邮件是当今的一个主要问题。托管您的网络服务器的网站可能具有许多防火墙规则,阻止网页从其任何托管网站发送电子邮件。如果托管公司没有这样做,垃圾邮件发送者可以购买托管包,然后使用托管公司服务器使用网站主机站点垃圾邮件。



我的第一个猜测是外发电子邮件(即smtp)可能会被故意阻止。您可能需要与您的网站托管公司进行礼貌的讨论,并可能同意一些额外的条款。这可能会解决您遇到的问题,但要做好准备,对您的请求的回复可能是不只是没有,但不是没有!
Fifteen years ago you probably could have done this without any hindrance. Spam is a major problem nowadays. The site that hosts your webserver likely has mucho firewall rules that block web pages from sending email from any of their hosting sites. If the hosting company didn''t do this, a spammer could purchase a hosting package and then use the web host site to spam away using the hosting company servers.

My first guess is that outgoing email (ie. smtp) is likely blocked, intentionally. You will likely need to have a polite discussion with your web host company and maybe agree to some additional terms. This might fix the problem you have, but be prepared that the response to your request might be "not just no but HELL NO"!


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

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