PDA

Click to See Complete Forum and Search --> : [2.0] Send Email


Beast777
Jul 26th, 2007, 12:57 PM
I have an app that I want to send an email to a user on our network. I do not have access to the SNMP info but I have an app working in VB and the code is below. I keep trying to get it to work in C# but no success. I am close I think with the chunk of code below the VB. Can someone help?


'get recordset for email's body
Dim OlApp As New Outlook.Application
Dim OlNameSpace As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim strPath, strSubj As String
Dim cn As New SqlConnection
cn.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
"ource=""server"";persist security info=False;initial catalog=database"
cn.Open()
cmdtext = "Select * from VehicleReq2 where ReqNum = " & "'" & txtReqNum.Text & "'"
da.SelectCommand = New SqlCommand(cmdtext, cn)
Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
'get attachment - Current is a structure in the module

Try
msg = OlApp.CreateItem(Outlook.OlItemType.olMailItem)
msg.To = "Myemail@fakeserver.com"
msg.Subject = "Vehicle Request " & num
msg.Body = "Press Send to send now or add any comments to bottom. Do not change request data" & vbCrLf & vbCrLf
While dr.Read
msg.Body = "There is a new Vehicle Request. " & vbCrLf
msg.Body = msg.Body & "Request Number: " & dr.GetValue(0) & vbCrLf
msg.Body = msg.Body & "Date Ordered: " & dr.GetValue(1) & vbCrLf
msg.Body = msg.Body & "Crse Number: " & dr.GetValue(2) & vbCrLf
msg.Body = msg.Body & "DT Required: " & dr.GetValue(3) & vbCrLf
msg.Body = msg.Body & "DT Released: " & dr.GetValue(4) & vbCrLf
msg.Body = msg.Body & "Ordered by: " & dr.GetValue(5) & vbCrLf
msg.Body = msg.Body & "Sqn: " & dr.GetValue(6) & vbCrLf
msg.Body = msg.Body & "Local: " & dr.GetValue(7) & vbCrLf
msg.Body = msg.Body & "Type of Ex: " & dr.GetValue(8) & vbCrLf
msg.Body = msg.Body & "Ex Location: " & dr.GetValue(9) & vbCrLf


End While
msg.Display()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If

using Outlook = Microsoft.Office.Interop.Outlook;


Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace and Logon information.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

// Log on by using a dialog box to choose the profile.
oNS.Logon(null, null, true, true);


// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

// Set the subject.
oMsg.Subject = "Send Using OOM in C#";

// Set HTMLBody.
String sHtml;
sHtml = "<HTML>\n" +
"<HEAD>\n" +
"<TITLE>Sample GIF</TITLE>\n" +
"</HEAD>\n" +
"<BODY><P>\n" +
"<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
"</BODY>\n" +
"</HTML>";
oMsg.HTMLBody = sHtml;

// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("MyEmail@fakeserver.com");
oRecip.Resolve();

// Send.
oMsg.Display();

// Log off.
oNS.Logoff();

// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oNS = null;
oApp = null;

Beast777
Jul 26th, 2007, 01:01 PM
Actually, I found the problem. I misspelt the email but while I have your attention, does anyone know how to use the oMsg.Display()? I believe it needs something in the brackets but I do not know what.

Beast777
Jul 31st, 2007, 08:42 AM
Bump. Anyone use oMsg.Display? There must be some way to display the email and then have the user send it.

SomethinCool
Aug 1st, 2007, 09:57 AM
I just use oMsg.Show().. i believe that should work..

Beast777
Aug 1st, 2007, 11:24 AM
Unfortunately, it looks like .Show is not an option with VS2005 2.0 C#. Any other ideas?

RaviIntegra
Aug 2nd, 2007, 05:41 AM
try the below link

http://www.codeproject.com/aspnet/smtp_mail.asp

Beast777
Aug 2nd, 2007, 06:50 AM
I do not have access to SMTP.