asp.net如何发送邮件与类 (本地主机)?

发布时间: 2016/8/21 16:30:19
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

电子邮件 + 附件 (vCard) 发送。

我也不知道自己在做什么错,寻找类似的问题,为 2 天但无解问题计算器或谷歌适合我的描述。

因为我写我的代码习惯

发送邮件 + 附件类︰

using System;
using System.Data;
using System.Configuration;
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.Configuration;
using System.Net.Mail;
using System.Net.Configuration;
using Compulutions.Net;
using Compulutions.Web;
using System.IO;
using System.Web.Mail;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Services.Description;

// System.Web.Mail.SmtpMail.SmtpServer
// using System.Web.Mail.SmtpClient;


namespace vCardGenerator.Website
{
    public class SendvCard
    {
        public void MailvCard(string recipient, string filename)
        {
            Mailer smtp = new Mailer("localhost");

            /* SMTP - port 25 */

            smtp.AddAttachment(filename); //.vcf file Path
            smtp.FromAddress = new MailAddress ("random@mail.com");
            smtp.Subject = "vCard";
            smtp.MailBody = "Attachment is a vCard";
            smtp.AddRecipient(recipient);

#if !DEBUG
            try
            {
#endif
                smtp.SendMail();
#if !DEBUG
            }

            catch (Exception ex)
            {
                Response.Write("Exception Occured:   " + ex);
                    //Responds.Write("Sending vCard Failed! Please try again")
            }
#endif


            //  File.Delete(filename);

            //      vCard.vcf Delete after being send
                FileInfo DeleteFileInfo = new FileInfo(filename);

        /*  FileInfo DeleteFileInfo = new FileInfo("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
            if (DeleteFileInfo.Exists)
                File.Delete("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf"); */
        }
    }
}

按钮发送︰

protected void btnSend_Click(object sender, EventArgs e)
{
// Send mail with attachment
using (Attachment data = new Attachment("C:\\local\\vCardGenerator.Website\\" + "FirstName_LastName.vcf", MediaTypeNames.Application.Octet))
{

SendvCard smtp = new SendvCard();
}

lblStatus.Text = "Send to:" + " " + txtMail.Text;

FileInfo DeleteFileInfo = new FileInfo("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
if (DeleteFileInfo.Exists)
File.Delete("C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" +".vcf");
  }
 }
}

愿意提供任何其他/更多的信息,如果需要的话。

试图得到专业人士的帮助,所以至少给我一个解决方案前燃烧着我。

真诚,
绝望的生物。

解决方法 1:

看起来像只创建一个实例的 SendvCard 并不调用类中的方法发送 (MailvCard)

 SendvCard smtp = new SendvCard();

 smtp.MailvCard("user@domain.com", "filename"); // calls the method on the class
官方微信
官方QQ群
31647020