Problem with send mail

S

Sophie

Hi, I'm not very good in english so excuse me if I have difficulty to
explain my problem.

I want to send a mail with VB.net with this code :

Option Strict On

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
'Votre message
Dim objMessage As New System.Web.Mail.MailMessage
With objMessage
.From = txtFrom.Text
.To = txtTo.Text
'.Cc=
'.Bcc=
.Body = txtBody.Text
.BodyFormat = Web.Mail.MailFormat.Text
.Subject = txtSubject.Text
'.Attachments=
End With
'Quel serveur utiliser?
System.Web.Mail.SmtpMail.SmtpServer = txtServer.Text
'System.Configuration.ConfigurationSettings.AppSettings("SMTPserver")
pour choisir le smtp par défaut
'Envoi du message
System.Web.Mail.SmtpMail.Send(objMessage)
objMessage = Nothing
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub


But I obtain this error :

An unhandled exception of type 'System.Security.SecurityException' occurred
in system.windows.forms.dll
Additional information: Fail on the request for the permission of type
System.Web.AspNetHostingPermission, System, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089.

I did a test and use the same code in a ASP application and I can send my
mail. Si I suppose it's some security on my computer but I don't know what
and nobody can help me here. Do you know how
"system.web.mail.smtpmail.send" send the mail. What the right I need to use
this command?
Please help me I really need you!

Thank you
Have a nice day
Sophie
 
C

Cor Ligthert

Sophie,

To make this nice, this is something I made as sample once for a Polish guy,
I hope you find it well that I do not change it in French? (wiadomosch is
message) It is almost the same as yours I thought, however than you can
check it yourself.

\\\
Dim wiadomosc As Web.Mail.MailMessage = New Web.Mail.MailMessage
Dim config As MailAttachment = New
MailAttachment("c:\test1\test.txt")
wiadomosc.Attachments.Add(config)
wiadomosc.From = xxx@xxx
wiadomosc.To = xxx@xxx
wiadomosc.Subject = "..."
wiadomosc.Body = "..."
wiadomosc.BodyFormat = Web.Mail.MailFormat.Text
System.Web.Mail.SmtpMail.SmtpServer = "xxxx.xxxx.xx"
System.Web.Mail.SmtpMail.Send(wiadomosc)
///

The smtp server can be any smtp server that you can reach, when you have a
provider mostly even that one, altough he will probably not be lucky with
you when you use this for spam.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top