C
C#ā€¢3w ago
Bulelani Botman

How To Properly Catch An SMTPException & IOException

Hi, everyone. I have a project that sends emails to clients and also saves a copy of the email but somewhere during the operation of sending these emails, I get an IOException & sometimes an SMTPException for a particular email & it breaks the server & gives me a "Error 500" I want to properly handle an exception when happens, like retry sending that email again or just move on to the next or maybe log it (If logging that error is the way, I want a way for the problem to appear on Event Viewer). How would I achieve that. Here is the code link
Pastebin
Sending of Emails Code - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
16 Replies
HtmlCompiler
HtmlCompilerā€¢3w ago
i will admit this code has not the most readable names...
Pobiega
Pobiegaā€¢3w ago
Wow, that code is... something. But, to answer your question... You probably want a mix of railway programming and some resilliance patterns, like Polly https://github.com/App-vNext/Polly can help you with retry logic as for the railway stuff, the idea is that you need to figure out what the correct order to do things in, depending on your desired results If copying and saving the email doesnt work, should it still be sent? if the sending fails, should it still save? If the save works, but send fails, when it retries, should it save again? etc
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
I'm sorry. It makes sense when you know the business & all its rules šŸ˜­ , good or bad?
Pobiega
Pobiegaā€¢3w ago
Bad, I'm afraid.
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
jesus! i'm not much of a senior dev or anything of that sort & where i'm interning, i'm pretty sure that they don't adhere to dev standards that the whole world does to answer this question, i just want to purely handle the exceptions that may occur. if for instance it doesn't send, does my code with the retrying loop work for handling this error because you might find that the exception has to do with internet connection & it that case it doesn't send anything. if the exception is IO related, maybe the saved copy file doesn't get renamed, i don't mind but it should just save it & continue running. i just don't want to have an Error 500 pop out telling me it crashed
HtmlCompiler
HtmlCompilerā€¢3w ago
the point is you can't handle all the errors, at a certain point the procedure should stop and send a message to a human for manual intervention
Pobiega
Pobiegaā€¢3w ago
If you cant send the email because of the servers internet connection, you also cant talk to a database (unless that db is hosted locally, which is already a big ouch) I'm not sure an intern should be writing code to handle retries and sending emails for something this important - these seem to be legal notifications.... Something this important should be handled with tried and tested resilliance patterns - the task to send the email should be saved as a job where its status can be tracked and automaticall repeated upon failure. Why I called the code bad is that by my standard, this seems very.. "basic", for lack of a better word. it builds the message in the same place it tries to send it, all memorystreams everywhere, locally setting up the SMTP client with hardcoded addr... But as you said, you're an intern with little to no previous experience, so perhaps thats to be expected?
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
the database is hosted on the server as well
Pobiega
Pobiegaā€¢3w ago
Big ouch But okay. Make a table for keeping track of messages And their status. Only write it down as sent when you know it worked Then have a retry job that scans that table
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
yeah! it's some sort of notices for clients & the company doesn't have a senior nor a intermediate software who knows all the rights & wrongs of design patterns so the tasks are handled by interns & juniors who were also interns
Pobiega
Pobiegaā€¢3w ago
Fantastic :kekw:
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
šŸ˜­ , this is the best i could come up with, just basic stuff that's why i need a proper internship / graduate programme
Pobiega
Pobiegaā€¢3w ago
I agree It's hard to continue to grow if you don't have a senior to show you how to do harder things better
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
i was thinking of doing that, updating my db whenever an email is sent. pretty rudimentary
Pobiega
Pobiegaā€¢3w ago
Yup But please use efcore properly instead of sending manual sal queries to it
Bulelani Botman
Bulelani BotmanOPā€¢3w ago
use linq queires, i'll do that thank you! but any code suggestions / examples i should include in the meantime?
Want results from more Discord servers?
Add your server