如何用class(localhost)发送邮件?

时间:2012-10-31 15:36:56

标签: c# asp.net .net class visual-studio-2005

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

我不知道我做错了什么,在2天内搜索了类似的问题,但没有问题/解决方案在Stackoverflow或Google适合我的描述。

因为我差错地编写了我的代码

发送邮件+附件类:

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 个答案:

答案 0 :(得分:2)

看起来您只创建了SendvCard的实例,并且没有在类(MailvCard)中调用send方法。

 SendvCard smtp = new SendvCard();

 smtp.MailvCard("user@domain.com", "filename"); // calls the method on the class