Solved

show the mail before sending

Posted on 2006-07-23
19
312 Views
Last Modified: 2012-05-05
In ASP.NET 2.0 in C#
I prepare a mail to be send with all that is needed
but I don't want to send it automatically.
I want to show the mail on the screen, to let the user check it and then
the user will press the button of send.
This is my preparing of the mail. What do I have to change?
   
           MailAddress to = new MailAddress("anat@business-solutions.co.il");
            MailAddress from = new MailAddress("anat@business-solutions.co.il");
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

            message.Subject = "test email1";
            message.Body = "test email1 from business-solutions on 18/06/2006";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
             smtp.Send(message);
Thankyou
Anat
0
Comment
Question by:ANAT2403
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 3
19 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17162812
Hi ANAT2403,

What you could do is create the message object with page scope and do this in the page_load event:

MailAddress to = new MailAddress("anat@business-solutions.co.<wbr/>il");
            MailAddress from = new MailAddress("anat@business-solutions.co.<wbr/>il");
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

            message.Subject = "test email1";
            message.Body = "test email1 from business-solutions on 18/06/2006";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            Response.Write( message.Body );

then have a button on the page, that when clicked that sends it with:

smtp.Send(message);

Apresto
0
 
LVL 23

Expert Comment

by:apresto
ID: 17162813
What code do you have at the moment for the page
0
 

Author Comment

by:ANAT2403
ID: 17162873
Hi Apresto
This situation might happen while I have various codes in a page. I guess I should open a new page for this
because the response.write will overwrite the content of my page
Why did you say to put it in the page_load event?
Thanks
Anat

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:apresto
ID: 17162904
I just said page_load as an example, i meant put it somewhere where it would load when the page did, as i assumed that the information to be used in the message obvject would be submitted to this page from another.

You dont need to use response.write - you could populate a literal control or some other web control with the message body

Apresto
0
 

Author Comment

by:ANAT2403
ID: 17162927
But all this will not appear as mail.
I want it to appear as mail in outlook
Anat
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17162953
Do you want to integrate with Outlook, or have a web page that resembles the Outlook mail item form?

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163050
I would like to integrate with Outlook.
Anat
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163054
That is a messy proposition, since Outlook is COM, and by its very nature a single threaded environment, while ASP.NET is multi-threaded.  You need to be very certain that you can't accomplish this another way before going down the Outlook road.

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163062
Any how how I do it through Outlook?
Anat
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163080
1) You need Outlook installed on a server, if you are publishing from a machine to a remote machine

2) You need to add a COM reference for the Microsoft Outlook type library to the project

3) Use something like this:

  Public Shared Sub SendMail(ByVal [to] As String, ByVal subject As String, ByVal body As String, ByVal attachment As String)

    Dim app As New Outlook.Application
    Dim mail As Outlook.MailItem = app.CreateItem(Outlook.OlItemType.olMailItem)
    mail.To = [to]
    mail.Subject = subject
    mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText
    mail.Body = body
    mail.Display()

    app.Quit()

    System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mail)

  End Sub

4) Depending on the version of Outlook, and the COM wrapper that gets generated, you might have to fix that example code.

5) If you use Outlook, then you don't need all that SMTP stuff

Bob
0
 

Author Comment

by:ANAT2403
ID: 17163093
The thing is that in some mails I have to send it automatically with the SMTP and in some mails through the outlook.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17163103
Ok, I am not trying to dictate any course of action.  I assumed that you were replacing SMTP with Outlook.  If you need both, then keep it.

Bob
0
 

Author Comment

by:ANAT2403
ID: 17183517
Hi,
I want to use the code you sent me for opening the outlook but I need it in C# and I don't succeed
to tranform it to C#. I guess I need some using namespaces in the beginning.
Can you help?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17183648
Try this:

public static void SendMail(string toAddress, string subject, string body, string attachment)
{
    Outlook.Application app = new Outlook.Application()
    Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);
    mail.To = toAddress;
    mail.Subject = subject;
    mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
    mail.Body = body;
    mail.Display();

    app.Quit();

    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);

}

Bob
0
 

Author Comment

by:ANAT2403
ID: 17184119
I can't write the Outlook.Application. It is not recognized. I need some namespace. You know which one?
Anat
0
 

Author Comment

by:ANAT2403
ID: 17184225
Hi,
OK I undesrtood. I have to add Reference to COM Microsoft Outlook 11.0 Object Library.
I will continue now and inform you.
Anat
0
 

Author Comment

by:ANAT2403
ID: 17184988
mail.Display() force me to give a parameter.  (oject Modal)
What parameter can I give?

 
0
 

Author Comment

by:ANAT2403
ID: 17185219
Hi,
It sends the mail but it does not open the outlook . I want to open the outlook see the prepared mail, approve it
and then send it by pressing the send button of the outlook.
I would like to main the command
<a href="mailto:anat@business-solutions.co.il?subject=Your subject&to=anat@business-solutions.co.il&body=Enter your message here">Mail</a>
in the code behind so that I can give values to the body ,to and subject.
How do I do it?
0
 

Author Comment

by:ANAT2403
ID: 17185301
Ok I ask the question and answer them.
In the display i had to write display(true)
OK it works OK.
Thankyou
Anat
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question