0

I have a problem. I coded a program which lets you send an email to the address from a .txt list. It works (I'm in Germany) but for other people (in other countries) it doesn't work. For them only email sending over Gmail works. Any help?

Here is the code :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void cmdDistribute_Click(object sender, EventArgs e)
    {
        FileStream fs;
        StreamReader sr;

        if (Receiverlist.Text == "")
        {
            MessageBox.Show("Please type in the path of the list with the receiver! (.txt)", "ERROR : CANT FIND RECEIVERLIST!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        fs = new FileStream(Receiverlist.Text, FileMode.Open);
        sr = new StreamReader(fs);

        string zeile;

        if (senderID.Text == "")
        {
            MessageBox.Show("Please type in your login information!", "ERROR : NO EMAIL ADDRESS!");
            fs.Close();
            return;
        }

        else if (SenderPassword.Text == "")
        {
            MessageBox.Show("Please type in your login information!", "ERROR : NO PASSWORD!");
            fs.Close();
            return;
        }

        MessageBox.Show("While sending the emails this programm won´t  response till it has send \nall emails! This could take a while, so please be patient...", "ALERT", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        string mailProvider = "";
        int port = 0;

        try
        {
            if (ProviderGmail.Checked)
            {
                mailProvider = "smtp.gmail.com";
                port = 587;
            }
            else if (providerHotmail.Checked)
            {
                mailProvider = "smtp.live.com";
                port = 587;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can´t connect to the GMail / Hotmail server. \n Please contact TheFlash on skype!", "ERROR : SMTPSERVER", MessageBoxButtons.OK, MessageBoxIcon.Error);
            fs.Close();
            return;
        }

        if (EmailSubject.Text == "")
        {
            MessageBox.Show("Please type in the subject of the email!.", "ERROR : EMPTY SUBJECT", MessageBoxButtons.OK, MessageBoxIcon.Error);
            fs.Close();
            return;
        }

        int n = 1;

        while (sr.Peek() != -1)
        {
            zeile = sr.ReadLine();

            try
            {

                System.Net.Mail.MailAddress DistributorMail = new System.Net.Mail.MailAddress(senderID.Text);
                System.Net.Mail.MailAddress Receiver = new System.Net.Mail.MailAddress(zeile);
                System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(DistributorMail, Receiver);
                email.Subject = EmailSubject.Text;
                email.Body = EmailBody.Text;

                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(mailProvider,port);
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(senderID.Text, SenderPassword.Text);

                if (checkBox1.Checked)
                {
                    if (EmailAttachment.Text == "")
                    {
                        MessageBox.Show("Please type in the path of your attachment!.", "ERROR : EMPTY ATTACHMENT PATH", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        fs.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("If you add an attachment to your mail, it will take longer to send it!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        email.Attachments.Add(new System.Net.Mail.Attachment(EmailAttachment.Text));
                    }
                }
                else
                {
                    EmailAttachment.Enabled = false;
                }

                try
                {
                    client.Send(email);
                    label9.Text = "Emails sent : " + n;
                    n = n + 1;

                    if (sr.Peek() == -1)
                        MessageBox.Show("Finished!", "TheFlash´s Email Distributor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception Ex)
                {
                    label9.Text = "Couldn´t send the emails!";
                    fs.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("This is not an valid email address.", "ERROR : WRONG EMAIL FORMAT", MessageBoxButtons.OK, MessageBoxIcon.Error);
                fs.Close();
                return;
            }

        }
        fs.Close();
        return;

    }

    private void cmdExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void cmdAbout_Click(object sender, EventArgs e)
    {
        new AboutBox1().Show();
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
iAmFastAndYou
  • 63
  • 1
  • 1
  • 6
  • Are you connecting with TLS or just basic SSL? – Mike Precup Jun 01 '13 at 20:34
  • Please read [this answer](http://meta.stackexchange.com/a/130208/213671) to understand why I have edited your title. – gunr2171 Jun 01 '13 at 20:35
  • @MikePrecup connect with basic SSL, its in the code man... – iAmFastAndYou Jun 01 '13 at 20:36
  • I went and did some research, you actually are using TLS: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx , which is good, as I believe hotmail requires it. There are two ways of using SSL, and all your code has is a flag for EnableSsl, so I asked. No need to treat me like I didn't read the question. – Mike Precup Jun 01 '13 at 20:41
  • @MikePrecup sorry sir i dont wanted to treat you :/ I am just so confused, im working on this shit now since yesterday (no sleep) :( Could you give me a code example to fix this problem? – iAmFastAndYou Jun 01 '13 at 20:43
  • Do you have a stacktrace for the users using hotmail? You should provide more details about what "doesn't work" means. – Travis Jun 01 '13 at 21:24
  • @Travis So, the thing is : I live in germany, and i can send the emails over Gmail AND Hotmail. I have a friend in the USA and he tried to send his mails to, but he can only send his mail over Gmail, not over Hotmail. How you can see in my code, i added a few checkbox. If you check the hotmail checkbox, the smtp server and port will get the information about the hotmail smtp server and port. If he tries to send his mail over hotmail, he gets an error which I coded (try,catch) see at the bottom of the cmdDistribute Button. Please help me, i have tried everything.... – iAmFastAndYou Jun 01 '13 at 21:29
  • 2
    @iAmFastAndYou In the try/catch for the sending, you swallow the exception. You should (somehow) log the message and/or the stacktrace before returning. Hopefully that will provide more information about what the problem is. – Travis Jun 01 '13 at 21:42
  • @Travis please give me an example code... im really tired of this, trying to fix this bug for 2 days now (without sleep -.-) – iAmFastAndYou Jun 01 '13 at 21:57
  • In the `catch` block, concatenate `ex.ToString()` onto the `MessageBox` or `Label`. Then get your user to send you a screenshot of the new long *scary-looking* detailed error message and post it on here. – davmos Jun 01 '13 at 22:13
  • @davmos Ok sir I will do that tomorrow because I didnt sleep for over 2 days now. I will change it tomorrow and post it here, so please throw an eye on this Question. Ah and there is a new problem too, now Hotmail wont work for me too :( – iAmFastAndYou Jun 01 '13 at 22:20
  • OK @iAmFastAndYou, will do. There is a thread [here](http://answers.microsoft.com/en-us/windowslive/forum/hotmail-email/unable-to-send-from-gmail-via-smtplivecom/feb1f27b-de60-4d96-8590-7cc51b6f990c) with many hacked off people also having problems sending mail via `smtp.live.com`. There are many comments with people still having problems now. – davmos Jun 01 '13 at 22:27
  • @davmos Hello sir, now I woke up and I will change the code so we will see the error. I will post it right here. – iAmFastAndYou Jun 02 '13 at 07:37
  • @davmos Hello sir! This is the incoming error : http://tinypic.com/view.php?pic=2s9yo3r&s=5 – iAmFastAndYou Jun 02 '13 at 08:03

1 Answers1

0

OK, for one reason or another it seems like your user may have been locked out of their Hotmail account. So they could try logging in via the normal web page and see if they are asked to solve a puzzle to unlock it. For more info on this, see:

Why am I repeatedly locked out of my Hotmail account, and what can I do about it?

I'm not sure if it is possible to code around this. There have been past questions you can look at here:

SMTP Client Sending error in C# app

Send email via Hotmail to gmail

Also see:

Sending Email From C# Via Hotmail/Outlook/Windows Live

facing issues while sending mail using smtp.live.com in c#

Community
  • 1
  • 1
davmos
  • 9,324
  • 4
  • 40
  • 43
  • Thank you,that could be the problem. I was a bit confused because both of them had the problem :/ And i tried more than 2 days without sleep to fix that hahahaha omg im so stupid^^ Thank you again Sir,have a nice day! – iAmFastAndYou Jun 02 '13 at 09:06
  • You're welcome. Don't feel stupid :) It's best if you don't swallow exceptions like that, as they will be your friend when it comes to diagnosing/fixing issues. You have a good day too. – davmos Jun 02 '13 at 09:09
  • For sure don't feel stupid. I had this issue too, which is why I wrote the blog post in the also see section of the answer. It's easy to miss. – Levi Botelho Jun 03 '13 at 09:13