简体   繁体   English

C#使用1个连接在线程中发送电子邮件

[英]c# Sending emails in threads using 1 connection

I have a program for sending my resume to emails of people who posted jobs if they included their email in their post. 我有一个程序,可以将简历发送到发布职位的人的电子邮件中,如果他们在其职位中包含了他们的电子邮件。

so I send an email with their quote of the job description, date of when it was generated etc, 因此,我发送了一封电子邮件,其中包含他们对工作描述的报价,生成日期等,

so each email is unique, But each email uploads the same file (resume.pdf) as Attachment. 因此,每封电子邮件都是唯一的,但是每封电子邮件都与附件上载相同的文件(resume.pdf)。

right now each time I send an email I need to upload the same file (resume.pdf) // my resume 现在每次发送电子邮件时,我都需要上传相同的文件(resume.pdf)//我的简历

so this are my questions: 所以这是我的问题:

  1. can I send each email and only upload my pdf resume once? 我可以发送每封电子邮件并且仅上传我的PDF简历一次吗?

  2. right now I use a smtp client library like this: 现在,我使用这样的smtp客户端库:

    GMailSmtp gmail = new GMailSmtp(new NetworkCredential("username", "password"));

so each time I send an email I create a thread that opens a new connection which seems time consuming to me. 因此,每次发送电子邮件时,我都会创建一个线程来打开一个新连接,这对我来说似乎很耗时。

I was wondering if there is an API or library to create 1 connection and then send all the emails I want thru a queue or create a new thread just for sending the email. 我想知道是否有一个API或库来创建1个连接,然后通过队列发送我想要的所有电子邮件,或创建一个仅用于发送电子邮件的新线程。

Yes. 是。 If you're using a third-party server like Gmail, you will need to upload your resume with each email. 如果您使用的是第三方服务器(例如Gmail),则需要在每封电子邮件中上传简历。 BUT, there are lots of easy ways to do this in the background. 但是,在后台有很多简单的方法可以做到这一点。

Play with this for a while and if you have specific questions or problems, post your code and your specific issue: 玩一会儿,如果您有特定的问题或疑问,请发布您的代码和特定的问题:

List<string> recipients = new List<string>();
    BackgroundWorker emailer = new BackgroundWorker();
    public void startSending()
    {
        emailer.DoWork += sendEmails;
        emailer.RunWorkerAsync();
    }
    private void sendEmails(object sender, DoWorkEventArgs e)
    {
        string attachmentPath = @"path to your PDF";
        string subject = "Give me a JOB!";
        string bodyHTML = "html or plain text = up to you";
        foreach (string recipient in recipients)
        {
            sendEmail(recipient, subject,bodyHTML,attachmentPath);
        }

    }
    private void sendEmail(string recipientAddress, string subject, string bodyHTML,string pathToAttachmentFile)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("your_email_address@gmail.com");
        mail.To.Add(recipientAddress);
        mail.Subject = subject;
        mail.Body = bodyHTML;

        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment(pathToAttachmentFile);
        mail.Attachments.Add(attachment);

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

    }

Note that BackgroundWorker requires a reference to System.ComponentModel. 请注意,BackgroundWorker需要对System.ComponentModel的引用。

You could use separate thread for sending e-mails. 您可以使用单独的线程发送电子邮件。 For example you can do it as Shannon Holsinger suggested. 例如,您可以按照Shannon Holsinger的建议进行操作。 You could also upload your resume to Dropbox or anywhere else and send the link instead of attaching a file. 您也可以将简历上传到Dropbox或其他任何地方,然后发送链接而不是附加文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM