本文介绍了Web.mail.attachment发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我在附件行收到错误的无效邮件附件"1.xml".
谢谢
Hello
I got the error Invalid mail attachment ''1.xml'' at attachment line please .
Thanks
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
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.Web.Mail;
public partial class Web_careers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// clearfields();
}
public void clearfields()
{
txtName.Text = "";
txtEmail.Text = "";
txtcomment.Text = "";
txtapply.Text = "";
}
protected void imgsubmit_Click(object sender, ImageClickEventArgs e)
{
string sHost = System.Configuration.ConfigurationManager.AppSettings["Host"].ToString();
string sUserName = System.Configuration.ConfigurationManager.AppSettings["AutorptUser"].ToString();
string sPassword = System.Configuration.ConfigurationManager.AppSettings["AutorptPassword"].ToString();
string sFromEmail = System.Configuration.ConfigurationManager.AppSettings["AutorptFromEmail"].ToString();
int nPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString());
bool fSSL = true;
string MessageBody = "";
string result = "";
try
{
System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
if (fSSL)
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
if (sUserName.Length == 0)
{
//Ingen auth
}
else
{
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
}
string file = fuResume.PostedFile.FileName;
MessageBody = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("../web/careers.txt"));
MessageBody = MessageBody.Replace("$name$", txtName.Text);
MessageBody = MessageBody.Replace("$email$", txtEmail.Text);
MessageBody = MessageBody.Replace("$ApplyFor$", txtapply.Text);
MessageBody = MessageBody.Replace("$coments$", txtcomment.Text);
MessageBody = MessageBody.Replace("$Attachment$", file);
System.Web.Mail.MailAttachment attachment;
attachment = new System.Web.Mail.MailAttachment(file);
Mail.To = txtEmail.Text;
Mail.From = sFromEmail;
Mail.Subject = "Enquiry";
Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
Mail.Body = MessageBody;
Mail.Attachments.Add(attachment);
System.Web.Mail.SmtpMail.SmtpServer = sHost;
System.Web.Mail.SmtpMail.Send(Mail);
result = "MailSuccess";
}
catch (Exception ex)
{
result = ex.Message;
}
}
}
推荐答案
这篇关于Web.mail.attachment发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!