Jump to content

Photo

C# [Need help please]

Software programming

  • Please log in to reply
15 replies to this topic

#1 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 12:42 AM

Hey,
is there an easy way to send an email through c#? like your email in one textbox, my email in anouther textbox and the email itself in anouther textbox?
thanks.

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#2 Coconut Man

Coconut Man

    Gigabyte

  • Members
  • 798 posts
  • LocationThe latest Smash Major

Posted 15 January 2013 - 12:44 AM

No idea, sorry. However, I don't know your timezone, but if you are in the same as mine, it's 1 43 or so right now, so try waiting till most people are awake xD

fl9Uov4.gif


#3 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 12:46 AM

No idea, sorry. However, I don't know your timezone, but if you are in the same as mine, it's 1 43 or so right now, so try waiting till most people are awake xD


my timezone is (UTC+10:00) conberra, melbourne, sydney. its 5:46pm now.
but thanx.

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#4 Coconut Man

Coconut Man

    Gigabyte

  • Members
  • 798 posts
  • LocationThe latest Smash Major

Posted 15 January 2013 - 02:12 AM

Oh wow xD sorry we'll hopefully you'll get more replies now that it's nearly morning here in the US

fl9Uov4.gif


#5 DaRatmastah

DaRatmastah

    Captain Overexcited Cyclops

  • Members
  • 463 posts

Posted 15 January 2013 - 06:49 AM

I'm not familiar with the inner workings of C#, but my guess is that you'd need to interact with a mail server somehow. Probably something like hMailServer.

#6 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 06:59 AM

I'm not familiar with the inner workings of C#, but my guess is that you'd need to interact with a mail server somehow. Probably something like hMailServer.

is there an easy way to use a email server in C#, cause i tried to google it but i couldnt find anything that makes sense?

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#7 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 15 January 2013 - 08:19 AM

It's very simple, but you need an smtp server to route it through. (you can use gmail's, yahoo's, whatever) I'll post a broken down code sample in a minute.

#8 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 15 January 2013 - 08:36 AM

this example is for g-mail, but you can change the host and port to whatever. (and turn off ssl if the host doesn't support it)
This requires two using statements at the top, System.Net and System.Net.Mail

MailAddress from = new MailAddress("youremailhere@gmail.com", "A display name here");
MailAddress to = new MailAddress("the receivers email here", "a display name here");
SmtpClient smtp = new SmtpClient() {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(from.Address, "your gmail password here")
};
using (MailMessage mess = new MailMessage(from, to)
   {
       Subject = "Subject field of email here",
       Body = "The body for the email here"
    })
{
    smtp.Send(mess);
}

First we need to set up two MailAddress objects - one for the sender and one for the receiver. The constructor takes the e-mail address and a display name (both as strings).

Next we need to set up an SmtpClient specific to google's server. Right after the new declaration we're using {} to initialize some values on the object, the only thing you need to change in this part is the NetworkCredential object - you need to specify the password to your g-mail account.

Next, we're use a using statement wrapped around the creation of a MailMessage. Again, we use an initializer to set the Subject and Body of the MailMessage. Inside of the using block we simply send the message. Voila.

#9 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 08:45 AM

this example is for g-mail, but you can change the host and port to whatever. (and turn off ssl if the host doesn't support it)
This requires two using statements at the top, System.Net and System.Net.Mail

MailAddress from = new MailAddress("youremailhere@gmail.com", "A display name here");
MailAddress to = new MailAddress("the receivers email here", "a display name here");
SmtpClient smtp = new SmtpClient() {
	Host = "smtp.gmail.com",
	Port = 587,
	EnableSsl = true,
	DeliveryMethod = SmtpDeliveryMethod.Network,
	UseDefaultCredentials = false,
	Credentials = new NetworkCredential(from.Address, "your gmail password here")
};
using (MailMessage mess = new MailMessage(from, to)
   {
	   Subject = "Subject field of email here",
	   Body = "The body for the email here"
	})
{
	smtp.Send(mess);
}

First we need to set up two MailAddress objects - one for the sender and one for the receiver. The constructor takes the e-mail address and a display name (both as strings).

Next we need to set up an SmtpClient specific to google's server. Right after the new declaration we're using {} to initialize some values on the object, the only thing you need to change in this part is the NetworkCredential object - you need to specify the password to your g-mail account.

Next, we're use a using statement wrapped around the creation of a MailMessage. Again, we use an initializer to set the Subject and Body of the MailMessage. Inside of the using block we simply send the message. Voila.



thanks, but im having a few problems. im not trying to sound stupid but where do you insert the code, how i understood it goes in a button but then i got an error? the error says:
"Error 1 The type or namespace name 'MailAddress' could not be found (are you missing a using directive or an assembly reference?) D:\C#\Email\Email\Email\Form1.cs 21 13 Email"

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#10 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 15 January 2013 - 08:47 AM

No offense but if what I said doesn't make sense you might want to pick up a book on C# or step through some simple tutorials. :x

#11 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 08:50 AM

No offense but if what I said doesn't make sense you might want to pick up a book on C# or step through some simple tutorials. :x


most of it makes sense but am i doing something wrong?

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#12 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 15 January 2013 - 08:52 AM

The error means you didn't put the using statements up top. At the top of your code there should be a few lines that read like
using System;
Somewhere up there you need to add
using System.Net;
using System.Net.Mail;


#13 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 09:07 AM

The error means you didn't put the using statements up top. At the top of your code there should be a few lines that read like

using System;
Somewhere up there you need to add
using System.Net;
using System.Net.Mail;


Thank you so much, its working. :)
(but for some reason its not sending a email to hotmail only gmail?)

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#14 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 15 January 2013 - 09:09 AM

It should send to hotmail fine. If it didn't give you a runtime error it should show up in your inbox eventually. (check your spam folder as well) The code above is synchronous so until the e-mail is done sending nothing else can happen - so once the UI is unlocked if there's no runtime error that means it sent appropriately.

#15 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 09:23 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace Email
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MailAddress from = new MailAddress("henkros@hotmail.com", "Display");
MailAddress to = new MailAddress("dhr7777777@hotmail.com", "Desplay");
SmtpClient smtp = new SmtpClient()
{
Host = "smtp.live.com",
Port = 995,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(from.Address, "Password")
};
using (MailMessage mess = new MailMessage(from, to)
{
Subject = "New Test C#",
Body = "Hello World"
})
{
smtp.Send(mess);
}
}
}
}

whats wrong with it?

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.


#16 Delta22

Delta22

    Kilobyte

  • Members
  • 139 posts
  • LocationAustralia

Posted 15 January 2013 - 09:42 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace Email
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MailAddress from = new MailAddress("henkros@hotmail.com", "Display");
MailAddress to = new MailAddress("dhr7777777@hotmail.com", "Desplay");
SmtpClient smtp = new SmtpClient()
{
Host = "smtp.live.com",
Port = 995,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(from.Address, "Password")
};
using (MailMessage mess = new MailMessage(from, to)
{
Subject = "New Test C#",
Body = "Hello World"
})
{
smtp.Send(mess);
}
}
}
}

whats wrong with it?



nevermind, i got it.
thank you so much

Programmer(in training), hacker(in minimal training), good with computers and mature, all you need to know about me but feel free to talk to me, contact me or whatever, i am no danger.






Also tagged with one or more of these keywords: Software, programming