Jump to content



Photo

Email from WPF C#


  • Please log in to reply
4 replies to this topic

#1 Sarkhan Vol

Sarkhan Vol

    Awesome

  • Joined: 14-October 12

Posted 01 January 2013 - 15:14

i've created an email app within a WPF but the program times out, i've tried it a few ways with different smtp clients and domains with no luck, please could someone shine some light on where i might be going wrong

Title="Email " Height="346" Width="336">
    <Grid>
	    <Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="181,256,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
	    <Label Content="To" Height="28" HorizontalAlignment="Left" Margin="24,16,0,0" Name="label1" VerticalAlignment="Top" />
	    <TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
	    <Label Content="From" Height="28" HorizontalAlignment="Left" Margin="24,50,0,0" Name="label2" VerticalAlignment="Top" />
	    <TextBox Height="23" HorizontalAlignment="Left" Margin="110,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
	    <Label Content="Subject" Height="28" HorizontalAlignment="Left" Margin="28,89,0,0" Name="label3" VerticalAlignment="Top" />
	    <TextBox Height="23" HorizontalAlignment="Left" Margin="110,94,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
	    <Label Content="Body" Height="28" HorizontalAlignment="Left" Margin="28,123,0,0" Name="label4" VerticalAlignment="Top" />
	    <TextBox Height="68" HorizontalAlignment="Left" Margin="71,154,0,0" Name="textBox4" VerticalAlignment="Top" Width="217" />
    </Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Net.Mail;
namespace Email_test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
	    public MainWindow()
	    {
		    InitializeComponent();
	    }
	    private void button1_Click(object sender, RoutedEventArgs e)
	    {
		    MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
		    System.Net.NetworkCredential auth = new System.Net.NetworkCredential("my@gmail", "mypassword", "gmail.com");
		    SmtpClient client = new SmtpClient("smtp.gmail.com",587);
		    client.EnableSsl = false;
		    client.UseDefaultCredentials = false;
		    mail.IsBodyHtml = true;
		    client.Credentials = auth;
		  
			 try {
		    client.Send(mail);
					 MessageBox.Show("Success" + Environment.NewLine + "Emai sent", "Close", MessageBoxButton.OK);
                  } 
            catch
				 {
					 MessageBox.Show("Error" + Environment.NewLine + "Email was not sent", "try again", MessageBoxButton.OK);   
				 }			 
  }
    }
}



#2 winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 03 January 2013 - 15:13

i've created an email app within a WPF but the program times out, i've tried it a few ways with different smtp clients and domains with no luck, please could someone shine some light on where i might be going wrong

Title="Email " Height="346" Width="336">
	<Grid>
		<Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="181,256,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
		<Label Content="To" Height="28" HorizontalAlignment="Left" Margin="24,16,0,0" Name="label1" VerticalAlignment="Top" />
		<TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
		<Label Content="From" Height="28" HorizontalAlignment="Left" Margin="24,50,0,0" Name="label2" VerticalAlignment="Top" />
		<TextBox Height="23" HorizontalAlignment="Left" Margin="110,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
		<Label Content="Subject" Height="28" HorizontalAlignment="Left" Margin="28,89,0,0" Name="label3" VerticalAlignment="Top" />
		<TextBox Height="23" HorizontalAlignment="Left" Margin="110,94,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
		<Label Content="Body" Height="28" HorizontalAlignment="Left" Margin="28,123,0,0" Name="label4" VerticalAlignment="Top" />
		<TextBox Height="68" HorizontalAlignment="Left" Margin="71,154,0,0" Name="textBox4" VerticalAlignment="Top" Width="217" />
	</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Net.Mail;
namespace Email_test
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}
		private void button1_Click(object sender, RoutedEventArgs e)
		{
			MailMessage mail = new MailMessage(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
			[b]System.Net.NetworkCredential auth = new System.Net.NetworkCredential("my@gmail", "mypassword", "gmail.com");[/b]
			SmtpClient client = new SmtpClient("smtp.gmail.com",587);
			client.EnableSsl = false;
			client.UseDefaultCredentials = false;
			mail.IsBodyHtml = true;
			client.Credentials = auth;
		  
			 try {
			client.Send(mail);
					 MessageBox.Show("Success" + Environment.NewLine + "Emai sent", "Close", MessageBoxButton.OK);
				  }
			catch
				 {
					 MessageBox.Show("Error" + Environment.NewLine + "Email was not sent", "try again", MessageBoxButton.OK);  
				 }			
  }
	}
}


It looks like one issue is in the text that I bolded above. Do you have an email address at Gmail my@gmail.com and a password "mypassword"?

EDIT: it won't let me bold it. :( It is the line of code with the "[b]" next to it.

#3 OP Sarkhan Vol

Sarkhan Vol

    Awesome

  • Joined: 14-October 12

Posted 03 January 2013 - 18:21

yes i do but i changed them for this post, for obvious reasons. i'm not sure if its an error with my code or the domian or smtp client and port details and was wondering if anyone else can get this to work?

#4 +scumdogmillionaire

scumdogmillionaire

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 23-October 01
  • Location: Denver, CO
  • OS: Windows 8.1 Pro/Windows 8.1 RT
  • Phone: Nokia Lumia 920

Posted 03 January 2013 - 18:31

yes i do but i changed them for this post, for obvious reasons. i'm not sure if its an error with my code or the domian or smtp client and port details and was wondering if anyone else can get this to work?


My apps send from gmail smtp clients all the time. Mine doesn't contain the domain param. Just auth = new NetworkCredentials("user@gmail.com", "password"); is all I do.

#5 OP Sarkhan Vol

Sarkhan Vol

    Awesome

  • Joined: 14-October 12

Posted 03 January 2013 - 20:15

great thx alot this worked it was the domain that made it time out... thx for the help