Example #1
0
        static void thr_smtp_con()
        {
            SmtpClient client = new SmtpClient("smtp.163.com");

            client.UseDefaultCredentials = true;
            client.Credentials           = new System.Net.NetworkCredential("账号@163.com", "密码");
            client.DeliveryMethod        = SmtpDeliveryMethod.Network;
            MailAddress from = new MailAddress("账号@163.com",
                                               " 电子科技大学第一莽夫" + (char)0xD8 + " 326啊江",
                                               System.Text.Encoding.UTF8);
            MailAddress to          = new MailAddress("账号@163.com");
            MailMessage mailMessage = new MailMessage(from, to);

            mailMessage.Body = "RNG凉了~~~";
            // Include some non-ASCII characters in body and subject.
            string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });

            mailMessage.Body           += Environment.NewLine + someArrows;
            mailMessage.BodyEncoding    = System.Text.Encoding.UTF8;
            mailMessage.Subject         = "15软件工程B" + someArrows;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
            client.SendCompleted       += new
                                          SendCompletedEventHandler(SendCompletedCallback);
            string userState = "test message1";

            send_str = " 开始发送邮件 \r\n";
            // current working directory.
            string     attachfile = "SMTP.jpg";
            Attachment atcData    = new Attachment(attachfile, MediaTypeNames.Application.Octet);
            // Add time stamp information for the file.
            ContentDisposition disposition = atcData.ContentDisposition;

            disposition.CreationDate     = System.IO.File.GetCreationTime(attachfile);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachfile);
            disposition.ReadDate         = System.IO.File.GetLastAccessTime(attachfile);
            // Add the file attachment to this e-mail message.
            mailMessage.Attachments.Add(atcData);
            SendMessage(main_wnd_handle, UPDATE_INFO, 100, 100);
            client.SendAsync(mailMessage, userState);
            MRE_check_end.WaitOne();
            send_str = " 邮件发送完成 \r\n";
            SendMessage(main_wnd_handle, UPDATE_INFO, 100, 100);
            mailMessage.Dispose();
        }
Example #2
0
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            String token = (string)e.UserState;

            if (e.Cancelled)
            {
                send_str = " 邮件发送取消 \r\n";
                SendMessage(main_wnd_handle, UPDATE_INFO, 100, 100);
            }
            if (e.Error != null)
            {
                send_str = string.Format("[{0}] {1}", token, e.Error.ToString());
                SendMessage(main_wnd_handle, UPDATE_INFO, 100, 100);
            }
            else
            {
                send_str = " 邮件已发送 \r\n";
                SendMessage(main_wnd_handle, UPDATE_INFO, 100, 100);
            }
            MRE_check_end.Set();
        }