Programmation
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
-50%
Le deal à ne pas rater :
-50% Baskets Nike Air Huarache Runner
69.99 € 139.99 €
Voir le deal

Programmation C#

Aller en bas

Programmation C# Empty Programmation C#

Message par Admin Mer 1 Mar - 10:43

Bonjour, je dois créer une application sur C# afin d'envoyer des mails.
Néanmoins, dès que le programme rencontre cette ligne lors du débogage, il m'affiche :"Un caractère non valide a été trouvé dans l'en-tête du courrier : '.'." (il m'affiche ce message pour la ligne        msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSmtp.Text.Replace("smtp", "@"), "Lucy", Encoding.UTF8) }; )
Voici mon code : Pouvez-vous me dire d'où vient l'erreur? (Dois-je corriger une ligne ou ajouter une bibliothèque?)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace Envoi_de_mail
{
   public partial class Form1 : Form
   {
       NetworkCredential login;
       SmtpClient client;
       MailMessage msg;

       public Form1()
       {
           InitializeComponent();
       }

       private void btnSend_Click(object sender, EventArgs e)
       {
           login = new NetworkCredential(txtUsername.Text, txtPassword.Text);
           client = new SmtpClient(txtSmtp.Text);
           client.Port = Convert.ToInt32(txtPort.Text);
           client.EnableSsl = chkSSL.Checked;
           client.Credentials = login;
           msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSmtp.Text.Replace("smtp", "@"), "Lucy", Encoding.UTF8) };
           msg.To.Add(new MailAddress(txtTo.Text));
           if (!string.IsNullOrEmpty(txtCC.Text))
               msg.To.Add(new MailAddress(txtCC.Text));
           msg.Subject = txtSubject.Text;
           msg.Body = txtMessage.Text;
           msg.BodyEncoding = Encoding.UTF8;
           msg.IsBodyHtml = true;
           msg.Priority = MailPriority.Normal;
           msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
           string userstate = "Sending...";
           client.SendAsync(msg, userstate);

       }

       private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
       {
           if (e.Cancelled)
               MessageBox.Show(string.Format("{0} send canceled.", e.UserState), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
           if (e.Error != null)
               MessageBox.Show(string.Format("{0} {1}", e.UserState, e.Error), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
           else
               MessageBox.Show("Your message has been succesfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
       }
   }
}

Admin
Admin

Messages : 2
Date d'inscription : 01/03/2017

https://programmationcsharp.kanak.fr

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum