【问题标题】:How to send mail with class (localhost)?如何使用类(本地主机)发送邮件?
【发布时间】:2012-10-31 15:36:56
【问题描述】:

电子邮件 + 附件 (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");
  }
 }
}

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

尝试寻求专业人士的帮助,至少在烧死我之前给我一个解决方案。

此致,
绝望的生物。

【问题讨论】:

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


    【解决方案1】:

    您似乎只创建了SendvCard 的一个实例,并没有调用(MailvCard) 类中的发送方法。

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

    【讨论】:

    • 谢谢,我已经编辑过了。但现在我的 web.config 中的邮件设置出现问题
    • @Rafael - 你将不得不发布一个关于你的新问题的新问题。请提供您收到的所有错误消息以及您的 web.config 的副本。
    • 好吧,那我必须这样做。谢谢(标记为已回答!)
    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2015-04-15
    • 2015-08-22
    • 2015-05-18
    相关资源
    最近更新 更多