在asp.net中发送电子邮件时出现问题 [英] Problem in sending email in asp.net

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

问题描述

我尝试发送带有文件附件的电子邮件

我在asp页面中使用Gmail SMTP

当实验和发送计算机时它的工作正常



当托管主机发送时,只有消息没有发送附件文件



代码

I try to send an email with a file attachment
I Use Gmail SMTP inside asp page
When the experiment and send of computer its''s working properly

When lifting on Hosting sends only the message is not sent the attached file

The Code

protected string UserName = "******@gmail.com";
protected string Pass = "*******";
protected string MailRes = "*******@gmail.com";


protected void Button1_Click(object sender, System.EventArgs e)
{

    using (MailMessage mailMessage = new MailMessage())
    {
        string Msgsb =
   "\n Name : " + txt_name.Text.Trim()
   + "\n Address : " + txt_address.Text.Trim()
   + "\n Message : " + txtBody.Text.Trim();

        mailMessage.From = new MailAddress(UserName);
        mailMessage.Subject = txtSubject.Text.Trim();
        mailMessage.Body = Msgsb;
        mailMessage.IsBodyHtml = false;
        mailMessage.To.Add(new MailAddress(MailRes));

        List<httppostedfile> files = (List<httppostedfile>)Session["Files"];
        foreach (HttpPostedFile file in files)
        {
            mailMessage.Attachments.Add(new Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType));
            Label1.Text = Label1.Text + file.FileName;

        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = mailMessage.From.Address;
        NetworkCred.Password = Pass;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mailMessage);

    }

推荐答案

首先,更改您的用户名 - 永远不要发布您的电子邮件地址在任何论坛,除非你真的喜欢垃圾邮件!如果有人回复你,你会收到一封电子邮件通知你。



其次,检查文件所在的位置 - C#代码在服务器上执行,而不是客户端,服务器根本无法访问客户端上的文件。这些文件需要在连接之前上传到服务器。



当你在开发中运行它时,服务器和客户端是同一台计算机,所以它看起来像喜欢它的作品。当您将网站上传到服务器时,它无法再访问文件。
First off, change your user name - never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.

Secondly, check where your file resides - C# code is executed on the server, not the client, and the server has no access at all to files located on the client. Such files need to be uploaded to the server before they can be attached.

When you run it in development, the server and client are the same computer, so it looks like it works. When you upload teh site to the server, it can''t access the files any more.


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

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