如何让我的应用程序检查并获取特定的电子邮件主题?

时间:2014-03-31 17:37:35

标签: c# email

我有这个发送电子邮件的课程。我使用我的isp电子邮件登录并将其发送到我的Gmail电子邮件。 现在我想这样做,当有人下载​​/获取我的应用程序时,它将检查我的gmail帐户地址每1分钟一个特定主题的特定电子邮件,因此电子邮件主题将像一个代码,例如电子邮件主题将是: 16765645

一旦应用程序下载了电子邮件,我想在textBox中显示电子邮件内容,并在文本文件中写入电子邮件内容。此外,我希望应用程序将在下载后检查电子邮件时间下载,以便应用程序不会一直下​​载相同的电子邮件。

这就是我今天发送电子邮件的方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DannyGeneral;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Diagnostic_Tool_Blue_Screen
{
    class SendEmail
    {
        private MailMessage photosmessage;
        public bool textfilessendended;
        public bool photossendended;
        Label lbl1;
        Label lbl3;
        Button SendLogFile;
        MailMessage message;
        MailMessage docmessage;
        public int timerdelay;
        public Timer timer3;

        public SendEmail(Label label2, Label label3, Button slf, int timerd, Timer timer)
        {
            textfilessendended = false;
            photossendended = false;
            lbl1 = label2;
            lbl3 = label3;
            SendLogFile = slf;
            timerdelay = timerd;
            timer3 = timer;
        }

        public void SendLogger()
        {
            string log_file_name = "logger.txt";
            string logger_file_to_read = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\log";
            string LoggerFile = Path.Combine(logger_file_to_read, log_file_name);
            try
            {
                MailAddress from = new MailAddress("test@gmail.com", "User " + (char)0xD8 + " Name",
                System.Text.Encoding.UTF8);
                MailAddress to = new MailAddress("test@test.net");
                message = new MailMessage(from, to);
                message.Body = "Please check the log file attachment i have some bugs.";
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "Log File For Checking Bugs" + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                Attachment myAttachment = new Attachment(LoggerFile, MediaTypeNames.Application.Octet);
                message.Attachments.Add(myAttachment);
                SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
                ss.SendCompleted += new SendCompletedEventHandler(ss_SendCompleted);
                ss.EnableSsl = true;
                ss.Timeout = 10000;
                ss.DeliveryMethod = SmtpDeliveryMethod.Network;
                ss.UseDefaultCredentials = false;
                ss.Credentials = new NetworkCredential("meuser", "mepassword");
                string userState = "test message1";
                ss.SendAsync(message, userState);
                lbl3.Enabled = true;
                lbl3.Visible = true;
                lbl3.BackColor = Color.DarkSeaGreen;
                lbl3.Text = "Sending email please wait";
                SendLogFile.Enabled = false;
            }

            catch (Exception errors)
            {
                Logger.Write("Error sending message :" + errors);
            }
        }

        private void ss_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            timerdelay = 0;
            timer3.Start();
            String token = (string)e.UserState;
            if (e.Cancelled)
            {
                Logger.Write("[{0}] Send canceled." + token);
                SendLogFile.Enabled = true;
            }
            if (e.Error != null)
            {
                lbl3.Enabled = true;
                lbl3.Visible = true;
                lbl3.BackColor = Color.DarkSeaGreen;
                lbl3.Text = "There was a problem with sending the log file please try again later and check the log file for more information";
                Logger.Write("There was a problem with sending the log file please try again later and check the log file for more information :" + e.Error.ToString());
                SendLogFile.Enabled = true;
            }
            else
            {
                SendLogFile.Enabled = true;
                message.Dispose();
                lbl3.Enabled = true;
                lbl3.Visible = true;
                lbl3.BackColor = Color.DarkSeaGreen;
                lbl3.Text = "Email have been sent successfully";
                Logger.Write("Attached log file have been emailed successfully");
                lbl1.BringToFront();
                lbl1.Visible = true;
                lbl1.Text = "The log file have been sent on: " + DateTime.Now;
            }
        }

通常的想法是使用像服务器这样的电子邮件来传输更新等推送消息。因此,当用户下载我的程序并在任何X分钟时运行新电子邮件时,他将在程序中看到它,如新的更新。

1 个答案:

答案 0 :(得分:1)

  

通常的想法是使用像服务器这样的电子邮件来传输更新等推送消息。因此,当用户下载我的程序并在任何X分钟时运行新电子邮件时,他将在程序中看到它,如新的更新。

老实说,这听起来更像是网络服务的工作,而不是电子邮件客户端。您可以让客户端定期使用发布最新更新消息的Web服务。实现起来要简单得多,there is a plethora of documentation这样做。

如果您让Web服务为您完成所有繁重工作,您将需要编写更少的代码,并且不必担心制作安全,稳定的解决方案。使用电子邮件来做这件事似乎非常糟糕。